Re: Way for computing random primes in standard C.



"Rod Pemberton" <do_not_have@xxxxxxxxxxxxxxxxxxx> writes:
"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:lnzmkefzv3.fsf@xxxxxxxxxxxxxxxxxx
[...]
So you're asserting that repeatedly calling srand() improves the
randomness of the numbers returned by rand()? In particular, you're
claming that that something like this:
<snip>
is likely to yield better (more random) results than if the second
call to srand() were removed (perhaps with a different number of
calls)?

KEITH: NO! Completely incorrect! This is the fifth time and last time.
Since I'm tied of trying to get through to you, I'll just repeat what I
posted to Sinaur. If you don't comprehend, you can deal with your
inabilities in private.

"As I've stated previously, the randomness is in the non-perfect algorithm
in rand(). But, the set of numbers generated by rand() is affected by
srand(). srand() doesn't affect the randomness of values that rand()
generates, it only changes the set of generated numbers. Since the
algorithm isn't a perfect-random number generator but a pseudo-random number
generator, the probabilities of certain numbers occurring is higher than
others. These probabilities can be shifted by calls to srand(). "

The fact that rand() is a pseudo-random number generator does not
imply that certain numbers appear with a higher probability than
others. For example, if RAND_MAX==32767, it's entirely possible that
each of the 32768 possible values will appear with exactly equal
probability over the long run, for each possible seed. (The generator
can still be imperfectly random even if this is true. For example,
rand() is likely to repeat over a cycle whose length depends on the
size of its internal state; a true random number generator would not
do so, though over the very long run it will sometimes appear to do
so.)

Or have I misunderstood what you meant by "the probabilities of
certain numbers occurring is higher than others"?

In your initial contribution to this thread, you wrote:

] The algorithm which generates a semi-random or pseudo-random number
] sequence has some internal initial values. If you don't call
] srand(), the sequence will be semi-random but will be the same
] sequence every time you run your program. So, if you were to write
] a card playing program, you might call srand() at every shuffle to
] start a new semi-random sequence and call rand() to generate the
] deck of cards. The "randomness" comes from the algorithm in rand()
] not from the starting values in generated by srand().

My response was that it would make more sense to call, say,
srand(time(NULL)) exactly once at program startup, and use successive
values from the *same* pseudo-random sequence for successive shuffles.
You said I was wrong.

You seem to be claiming that calling srand() *again* for each shuffle
is somehow better than calling srand() exactly once at program startup
and generating all shuffles from the single resulting pseudo-random
sequence. (By "calling srand()", I mean "calling the srand function
with some appropriate argument, such as srand(time(NULL))".) Is that
in fact what you've been claiming? In what sense is calling srand()
repeatedly better than calling it only once? How is starting a new
pseudo-random sequence better than continuing to use the original one?

Note that inserting an extra call to rand() will also change the
behavior of subsequent calls to rand(); would that suit your purpose
as well?

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



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: 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: Random numbers something insatiable ?
    ... A sequence of machine codes is executed - the same way any other formula is ... the big question to start with is - which and what is the "true" rand ... Anyway, randomness is a property which is an implicit property of, e.g., ... This behavior is called correlation, ...
    (comp.lang.cpp)
  • 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)