Archived

This topic is now archived and is closed to further replies.

Rob Loach

Random Numbers

Recommended Posts

I've been exploring different ways to get random numbers and I came to the conclusion that rand() with a seed of the return value of GetTickCount() (from the windows.h include) is the best (better then time(NULL) ). But then, I looked at what Quake 2 and 3 did. They have the following:
#define random()	((rand () & 0x7fff) / ((float)0x7fff))
#define crandom()	(2.0 * (random() - 0.5))
  
From my understanding, they seed it and then keep the seed in memory for later usage. When a random number is needed, they call crandom() and it returns a float value between 0 and 1 (to my understanding). What are some great solutions you guys came up with for random numbers? I'm now thinking of making my own random number header file/class. Any tips before I proceed?
Rob Loach Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore "The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - The Boondock Saints [edited by - Rob Loach on July 1, 2003 12:15:57 AM]

Share this post


Link to post
Share on other sites
is it possible for one function to be "more" random than another, given an output range? If so what does this mean? I''m guessing a function which more closely approximates the uniform distribution is the more random-er.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by ggoodwin37
is it possible for one function to be "more" random than another, given an output range? If so what does this mean? I''m guessing a function which more closely approximates the uniform distribution is the more random-er.


It is possible for one function to be more random than another but no mathematical equation is truly random, if you could imitate the chaos theory you would be very rich indeed!

quote:
Original post by Rob Loach
I looked at what Quake 2 and 3 did. They have the following:


#define random() ((rand () & 0x7fff) / ((float)0x7fff))#define crandom() (2.0 * (random() - 0.5))



This is a very typical equation to generate random numbers.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
no mathematical equation is truly random


Well, if you want to be that technical, nothing is truly random. The number of cokes you drink tonight (depends on how your feeling, your thirst, etc), the number of ants outside on your driveway right now (depends on the ants'' instincts, etc).... Everything is based on a very large amount of complex variables and algorithms.

But, back to the topic at hand, I''m just wondering if any of you have any tips on setting up a random number header file/class. I was thinking of setting up a function which has a static variable which would provide the seed so that it only initiates the seed the first time the function is called.



Rob Loach
Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?"
- The Boondock Saints

Share this post


Link to post
Share on other sites