Advertisement

Randomizing things

Started by March 08, 2005 04:57 AM
6 comments, last by Miserable 19 years, 8 months ago
hi im using rand() al ove my project but i noticed each time i run he program again it does the same sequance i have thought of using the following to set it according to the clock t = gettime(); for (int g=0;g < t;g++) f=rand() in pascal we would just call Randomize(); isnt there and equal function in ogl
----------------------------

http://djoubert.co.uk
What your afer is srand() which seeds the random number generator. With the same seed rand() will always give out the same set of numbers so you need to set a different seed each time your app is run. You were really on track with the clock idea :P This is the standard way:

srand( (unsigned)time( NULL ) );

you need to #include <ctime> i think to get the time() function. Run this once right at the start of your app and it will seed your random number generator and give you a different set of (seemingly) random numbers each time. You can seed it with whatever you like though ... gettime() might work fine (i've never tried it myself) but the way shown in every example ever is the time(NULL) way!
Advertisement
Yes thanks, it was so simple all along!!!!

I think i when u install the game it will save in the config file the seed acording to the time, and then every execution time it will increase the seed thus i can re-construct certain errors in the game by simple turning the seed back by one.

Or else the timer is perfect, so srand(argument) can take any signed integer right? or unsigned long integer?

i just want to know at which point i must wrap my seed
----------------------------

http://djoubert.co.uk
i think it can be any real number... but the problem with rand() is that it makes pseudorandom numbers from 0 to 256... if you'd want more "randomness", try and add more rand()s or use multiple rand()s to make numbers between 0 and 256256.
The world isn't unpredictable. It's CHAOTIC.
Quote: Original post by Intamin AG
i think it can be any real number... but the problem with rand() is that it makes pseudorandom numbers from 0 to 256... if you'd want more "randomness", try and add more rand()s or use multiple rand()s to make numbers between 0 and 256256.

Where on Earth did you get that idea? rand() generates integers in the range [0,RAND_MAX), where RAND_MAX on the machine I'm sitting at right now is 2,147,483,647.

On a side note, if you ever do try to get "more randomness" by calling rand() more than once, think carefully. Combining the result of two separate calls, for instance, will often not provide a uniform distribution (depending, of course, on the combination function).
@Intamin AG:
I dont think so. it returns (pseudo-)random numbers between 0 and RAND_MAX
On my system RAND_MAX is #defined to be 0x7fff = 32767
Advertisement
how can i read RAND_MAX Realtime?
----------------------------

http://djoubert.co.uk
RAND_MAX should be defined in the same header as rand()—<stdlib.h> if you are using C, or <cstdlib> if you are using C++.

This topic is closed to new replies.

Advertisement