JNY said:
I am using random to generate random numbers, thus:
int x,y;
for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}
When I run this program, 5 random numbers are produced. However, if I
stop the program and re-run it, the same sequence is produced. Am I
forgetting to do something? Is there another random number generator
which I could try?
1) 'random' is not the name of the standard library function for
generating pseudo-random numbers. That function is named 'rand'
2) If rand is used without first seeding with the srand function, it
will provide the same sequence on every run. This is a GoodThing(tm),
since it allows testing and debugging on the same sequence of values.
3) All this and more is covered in the FAQ
<
http://www.eskimo.com/~scs/C-faq/top.html>, in particular see the
sections on random numbers, including
13.15 I need a random number generator.
13.16 How can I get random integers in a certain range?
13.17 Each time I run my program, I get the same sequence of numbers
back from rand().
13.18 I need a random true/false value, so I'm just taking rand() % 2,
but it's alternating 0, 1, 0, 1, 0...
13.20 How can I generate random numbers with a normal or Gaussian
distribution?
You will note that 13.17 is just the question that you asked. Always
check the FAQ before posting. Steve Summit has put in a lot of work
making sure that reliable answers are available to common questions.
This ensures that you don't have to put up with incorrect answers from
the clueless and that you don't suffer the anger of people who see the
same, already answered questions appearing here every September, no
matter what month September is in.