• Advertisement
Sign in to follow this  

Random Numbers

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

If you intended to correct an error in the post then please contact us.

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?

Share this post


Link to post
Share on other sites
Advertisement
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 code
srand(time());
//Further initialisation

//...

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement