Generating Random numbers

Started by
11 comments, last by theObserver 21 years, 1 month ago
So far no one has posted the proper way to use rand() which is:
(double)rand() / (double)RAND_MAX

Using rand() % RAND_MAX is not recommended.

Also, rand()/RAND_MAX is integer division and returns 0


This will give you an integer >= min and < max:
(int)(min + ((double)rand() / (double)RAND_MAX * (max - min)));


-C
Advertisement
Just a correction to my previous post:

Using rand() % anynumber is not recommended.
Using rand() % RAND_MAX doesn''t make any sense.


-C
We''re sorry! You''re so perfect! We wish we could be more like you!

----
AIDS
----AIDS

This topic is closed to new replies.

Advertisement