Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Random number problem!

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1 iwoplaza   Members   

174
Like
0Likes
Like

Posted 19 June 2012 - 08:53 AM

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.

#2 fastcall22   Moderators   

10681
Like
2Likes
Like

Posted 19 June 2012 - 09:21 AM

You need to seed the random number generator when the program starts.
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#3 szecs   Members   

2942
Like
1Likes
Like

Posted 19 June 2012 - 09:24 AM

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 int. As you have it now.)

I'm ninja'd.

Edited by szecs, 19 June 2012 - 09:28 AM.


#4 iwoplaza   Members   

174
Like
0Likes
Like

Posted 19 June 2012 - 09:35 AM

Thanks :).
P.S. Now I understand how are seeds in minecraft working .)

#5 alnite   Members   

3412
Like
1Likes
Like

Posted 19 June 2012 - 12:52 PM

Posted Image

#6 tanzanite7   Members   

1410
Like
1Likes
Like

Posted 20 June 2012 - 06:20 PM

Posted Image:P




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.