Jump to content
  • Advertisement

Archived

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

Kalice

Random Number Generation.

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

Anyone know of a better way to get a better pseudo random number...currently this is what I'm using. edited: I suppose I should say that I'm using VC++ 6.0. but I think that was almost kinda obvious. //---------------------------------------------------------- srand( unsigned( time ( NULL ) ) ); //---------------------------------------------------------- then to get the "random" number. //---------------------------------------------------------- someVar = rand() % maxValofRandNum //---------------------------------------------------------- If anyone could point me to a website, or possibly post a small but somewhat more "random" generator, that would be great. The problem with this one, is that it's fairly predictable and you can time the stat roller to get near perfect stats (not good). Thanks for any response. --Kal Edited by - Kalice on 3/14/00 11:32:16 AM

Share this post


Link to post
Share on other sites
Advertisement
Ummm, if you can affect the sequence by timing, then I have to assume that you are calling srand() every time you call rand(). You should only call srand() once. The only other improvement I would suggest is using:

(double(rand())/RAND_MAX)*n

to get a number from 0 to n-1 instead of using %. The distribution should be better. This is straight out of Stroustrup (3rd. Edition). Incidentally, that book (The C++ Programming Language) also has a quick sample random number generator on page 686 that you might want to look at. If you''re truly interested in the topic, Bruce Schneier''s Applied Cryptography has more PRNG than I can count (a couple chapters worth).

-Brian

Share this post


Link to post
Share on other sites
That should be:
(double(rand())/(RAND_MAX+1)*n

I had what you originally have in a board game (range = 0-5 inclusive). I''d get a 6 if rand () returns RAND_MAX.

Share this post


Link to post
Share on other sites
You may want to check out this site:

http://www.swin.edu.au/astronomy/pbourke/software/random/

It has functions for Gaussian and Uniform random number generators

Share this post


Link to post
Share on other sites
Ok, thanks for the info. It is much appreciated. I''m not actually looking at any profession books. I took one class in College on programming, and am now really getting back into programming.

I am only calling srand once. I''m not quite sure how it loops like it does, but I''ve sat there and played with it, it''ll cycle through from the minimum range to the maximum range, so I''ve got a flawed "random" number generator.

I''ll take a look at what you suggested, but I''m at work right now. I''ll post again if I have any success...Thanks again.

--Kal

Share this post


Link to post
Share on other sites
Stoffel,
Yikes, never even thought of that. I''ll have to keep that in mind in the future, as that''s my standard technique for RNG. (Or I should get around to getting a good class-based RNG with better random properties than what ships with many compilers.

-Brian

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Big Thanks to osmanb and Stoffel (hope I spelled those right). It works in a much more amiable manner now and is not predictable. *woowoo*
Thanks for all the other info people provided as well.

Once again thanks much. It was really appreciated.

--Kal

Share this post


Link to post
Share on other sites
I apologize for the message before hand, I'm working off of multiple computers here...sometimes causes problems..=)


Big Thanks to osmanb and Stoffel (hope I spelled those right). It works in a much more amiable manner now and is not predictable. *woowoo*
Thanks for all the other info people provided as well.

Once again thanks much. It was really appreciated.

--Kal

Edited by - Kalice on 3/14/00 9:52:06 PM

Share this post


Link to post
Share on other sites
Search the web for something called "Mersenne Twister". One of the best and fastest RNGs out there.


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!