#### Archived

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

# Random Number Generation.

This topic is 6815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 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 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 on other sites
flipcode also has a random number generator in their programming resources.

##### 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 on other sites
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 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 on other sites
Search the web for something called "Mersenne Twister". One of the best and fastest RNGs out there.

---- --- -- -

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011388
• ### Who's Online (See full list)

There are no registered users currently online

×