Sign in to follow this  

Randomizing things

This topic is 4664 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

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

This topic is 4664 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this