• Advertisement
Sign in to follow this  

Random number problem!

This topic is 2042 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 making three-dimensional application in OpenGL, which generates a random blocks in the world (something like Minecraft). This is my script to generate blocks:


void GenBlocks(){
int currblock;
int currXline;
int currZline;
int randID;
for(currblock = 0,currXline = 1,currZline = 1;currblock < blockAM;currblock++,currXline++){
randID = rand() % 2 + 1;
if(currXline > pierw(blockAM)){
currXline = 1;
currZline++;
}
block[currblock].x = currXline*3;
block[currblock].y = 0;
block[currblock].z = currZline*3;
block[currblock].id = randID;
}
}


But every time when I run the application, it generates the same!

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Random number generators aren't really random.
That means they use some calculations, pre-defined arrays of pseudo-random numbers, whatever, you get the idea.
Every random number generators have a "randomize" function of some sorts, for example "srand". They take an initial value, often called as "seed". Using he same seed value will produce exactly the same sequence of numbers. If you don't call this initial srand, the generator will use a default seed I guess.

So, you need to call srand and pass a number to it. You would probably ask what can you use as seed, if you can't use a really random number in the first place.
Usually, system time is used for that, since it's in milliseconds and ever growing.
(what's cool about this thing is that if you save then load the seed number, you can regenerate exactly the same world from only an[color=#0000ff] int. As you have it now.)

I'm ninja'd. Edited by szecs

Share this post


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

  • Advertisement