# Random Numbers

This topic is 3946 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm currently working on a Match 3 style game, and I'm trying to create a grid (20x16) of coloured tiles. I first generate the grid then draw it later, but the problem comes from the random number generator (I believe). There are never more than two colours generated. My guess is that it generates the grid too fast for the time() (ctime library) function to get another value. For example: x = red o = blue xxxxx xxxxx xxxxo ooooo ooooo A typical grid might end up like this. Everything else works besides this, so are there any suggestions for a fix or workaround?

Post code.

##### Share on other sites
How are you generating your random numbers?

##### Share on other sites
Quote:
 My guess is that it generates the grid too fast for the time() (ctime library) function to get another value.

If I may hazard a guess of my own based on that comment, are you perhaps calling srand() before you generate each number? If so, rather call it only once, before you start using rand().

For example:

//initialisation codesrand(time());//Further initialisation//...//Generate three random numbers.  Note that srand() is not called here.val1 = rand();val2 = rand();val3 = rand();

##### Share on other sites
Here's the entire function.

     srand(GetTickCount());          // offsetX value is redeclared in second for loop     int offsetX = 0;     int offsetY = 125;     int randCol;     for (int y = 0; y < 18; y++)     {         offsetY += 25;                  for (int x = 0; x < 20; x++)         {             srand(GetTickCount());             randCol = (rand() % 3) + 1;             offsetX += 25;                          switch (randCol)             {                    // Red tile                    case 1:                           tempSprite = new Sprite(g_pBlockRedBitmap, drawBounds, BA_STOP);                           tempSprite->SetPosition(offsetX, offsetY);                           g_pGame->AddSprite(tempSprite);                           break;                                        // Green tile                           case 2:                            tempSprite = new Sprite(g_pBlockGreenBitmap, drawBounds, BA_STOP);                           tempSprite->SetPosition(offsetX, offsetY);                           g_pGame->AddSprite(tempSprite);                           break;                                               // Blue tile                    case 3:                           tempSprite = new Sprite(g_pBlockBlueBitmap, drawBounds, BA_STOP);                           tempSprite->SetPosition(offsetX, offsetY);                           g_pGame->AddSprite(tempSprite);                           break;                                        default:                            break;             }// randCol switch         } // x tile for loop                  offsetX = 0;              } // y tile for loop

##### Share on other sites
You should only call srand() once in your program.

##### Share on other sites
Move srand(GetTickCount()); up out of your for loops, and then see what changes you get in your generation.

##### Share on other sites
That was it. I originally put it in there because I had trouble with the for loops, but forgot to take it out when I realized that wasn't the problem. Thanks for the help.

##### Share on other sites
I think a small tutorial on random values is in its place.

A computer can't actually generate real randomness, however we might simulate random numbers. This is usually done by some form of mathematical function that as its input takes the previously generated number.
what the function srand does is provide the random-number-generator with the first "random"-value to use as input to the random-sequence. For the next rand()-value the output from the last call to rand() will be used. Usually you use the time() function as a, so called, seed to srand() as this is "almost" random. Or it will at least generate a seemingly random sequence each time the program is executed.

Hopefully that made some sense ;)

... if not this might help?

http://en.wikipedia.org/wiki/Pseudo-random_number_generator

##### Share on other sites
http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_rand.aspx

and of course rand_s :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 31
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015617
×