Advertisement

Random Number Generation.

Started by March 14, 2000 11:25 AM
8 comments, last by Kalice 24 years, 9 months ago
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
--Kal
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
Advertisement
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.
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
---Ranok---
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
--Kal
flipcode also has a random number generator in their programming resources.
Advertisement
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
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
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
--Kal
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!

This topic is closed to new replies.

Advertisement