Re: random is not random enough?

velthuijsen_at_hotmail.com
Date: 12/29/04


Date: 29 Dec 2004 07:26:24 -0800


JNY wrote:
> 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?

Random is not a basic C++ function. The ones you'd want to use are
srand and rand. because of this I'm going to assume a few things.
That is that under the hood this random calls srand once and then calls
rand after that.
If that is the case then your random does exactly what it is supposed
to do.
The rand function is a pseudo random generator. It starts from a seed
number (50 in this case) and based on that value returns you the next
value in a sequence (and uses the returned value to calculate the next
value of the sequence).



Relevant Pages

  • Re: rand() / (RAND_MAX / N + 1)
    ... information to allow you to write a program using rand and srand. ... > srand, I know what a seed is, but it is not clear to me what I have to do. ... This equation will generate s sequence of pseudo-random numbers. ... the sequence of numbers generated by the linear congruntail method will ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Way for computing random primes in standard C.
    ... randomness of the numbers returned by rand? ... call to srand() were removed (perhaps with a different number of ... the randomness is in the non-perfect algorithm ... ] sequence has some internal initial values. ...
    (comp.lang.c)
  • Re: random number geneator
    ... srand should be ... >> only oncein a program and NEVER inside a loop. ... After calling rand() the internal seed is updated ... >> this specific sequence. ...
    (comp.lang.cpp)
  • Re: Way for computing random primes in standard C.
    ... The srand function uses the argument as a seed for a new sequence ... If rand is ... internal state contains the result of srand[reasonable if it's ...
    (comp.lang.c)
  • Re: Quality of rand()
    ... in answer to your question the NIST suite uses the ... If the rand() implementation you are using passes the NIST tests, ... First of all, unless you call srand, you will notice that the sequence of ... If you do call srand() with a given value, ...
    (comp.lang.cpp)