# rand() not so random....

• 01-20-2007
Kewley
rand() not so random....
Everytime I run this program, that is supposed to display a random number between 0 and 20, it outputs 20. I would really appreciate if somebody could point out were I'm going wrong.

Code:

```#include <iostream.h> #include <cstdlib.h> using namespace std; int main() {  int mynumber = rand() % 10 + 20;  cout <<  mynumber;  cin.get(); }```
• 01-20-2007
twomers
srand( time(0) );

before rand();

FAQ

Unrelated, but use <iostream>, not <iostream.h>
• 01-20-2007
Opel_Corsa
Your program is using default seed values to generate rands. To produce real random values the best method is using the ctime library; this way since no two time values are the same you get perfectly random numbers.

Code:

```#include <ctime> srand((unsigned)time(0));```
• 01-20-2007
dwks
Quote:

To produce real random values the best method is using the ctime library; this way since no two time values are the same you get perfectly random numbers.
Not quite. Since time is ever-changing, using the time is a good way to generate a random number seed, such as that passed to srand. Using the time for a random number is a bad idea.

And anyway, rand() is a pseudo-random number generator -- it doesn't generate perfectly random numbers. It's very hard for computers to generate random numbers in general, and non-pseudo-random numbers in particular.

For most cases pseudo-random numbers a sufficient however.

Quote:

that is supposed to display a random number between 0 and 20
Well, it won't.
Code:

`int mynumber = rand() % 10 + 20;`
Read that as "Generate a random number, divide it by 10 and take the remainder (which will be from 0 to 9), then add 20." Accordingly, you should get a number from 20+0 to 20+9 -- 20-29 inclusive.

If you want a random number from 0 to 20 inclusive use
Code:

`rand() % 21`
[/edit]
• 01-20-2007
Cat
rand() is also pretty poor randomness on most compilers; most use some version of a linear congruential generator which is one of the worst (in terms of randomness) performing PRNGs. If you're making a professional program, try something like boost's mersenne twister PRNGs. Of course rand() works fine for simple things and testing, or where you don't care too much about how random things are.
• 01-20-2007
laserlight
You might want to be eternally confuzzled by Prelude concerning rand() before you take dwks' advice.
• 01-21-2007
p_tyo
Seeding..
What you have to do is seed the random number generator. Just do a google search for seed and rand() and you will get a pile of great links...
• 01-21-2007
avalanche333
here is a great way to generate random numbers:
Code:

```int getRandomInt( int iMin, int iMax ) {         return static_cast<int>( (iMax - iMin) * double(rand())/(RAND_MAX-1) + iMin ); }```

Change it to use whatever type you like
• 01-21-2007
vart
Quote:

Originally Posted by avalanche333
here is a great way

• 01-21-2007
Cat
avalanche's method is basically the one that laserlight's link recommended, apart from the fact it should be +1 and not -1 added to RAND_MAX.

However, any LCG will always have a stronger degree of serial correlation than many other PRNGs. This: http://upload.wikimedia.org/wikipedia/en/3/38/Randu.png is an example of a particularly poor (and once, a highly popular) LCG; when it generates triples of random points, all the points lie on one of fifteen different planes in the 3d space. Most LCGs are not as bad as that, but they all have the same phenomena, caused by a stronger than desired correlation between one output and the next subsequent output.
• 01-22-2007
Kewley
Thanks for all the help everyone. I've been trying out a few of the above and most work very well. Thanks again.
• 01-23-2007
Prelude