C
CBFalconer
Keith said:.... snip ...
Do you know of a specific C library implementation that uses an
algorithm in which rand() never returns 0?
No. But I never bothered looking.
Keith said:.... snip ...
Do you know of a specific C library implementation that uses an
algorithm in which rand() never returns 0?
Eric Sosman said:Keith said:AFAIK there is no specification for RAND_MIN, which will be at
least 1 for many implementations.
[...]
There is no RAND_MIN. The standard says that rand() "computes a
sequence of pseudo-random integers in the range 0 to RAND_MAX".
If an implementation's rand() function never returns 0, the implementation
is either non-conforming or of poor quality.
Do you know of an implementation in which rand() never returns 0?
The trouble is in the testing: If you sample three
hundred twenty-seven quadrillion rand() values and see
no zeroes, can you conclude that rand() will never, ever,
under any circumstances return a zero?
It's not necessary to program around poor implementations of rand(),
or even implementations that look like they might be poor. Instead,
it's easy enough to follow the recipe in Knuth's GraphBase book:
http://www-cs-faculty.stanford.edu/~knuth/sgb.html
and make a simple, fast, high-quality random number generator. And
you'll get the benefit that programs using it will get (assuming the
same seed values) the same stream of random numbers on different
platforms.
Michael said:At any rate, I agree with the basic point: if you're dissatisfied, or
think you might be, with rand(), better generators are not hard to
come by.
Tim Rentsch said:It's not necessary to program around poor implementations of rand(),
or even implementations that look like they might be poor. Instead,
it's easy enough to follow the recipe in Knuth's GraphBase book:
http://www-cs-faculty.stanford.edu/~knuth/sgb.html
It's not necessary to program around poor implementations of rand(),
or even implementations that look like they might be poor. Instead,
it's easy enough to follow the recipe in Knuth's GraphBase book:
http://www-cs-faculty.stanford.edu/~knuth/sgb.html
and make a simple, fast, high-quality random number generator. And
you'll get the benefit that programs using it will get (assuming the
same seed values) the same stream of random numbers on different
platforms.
Alternatively, George Marsaglia has posted code here for a couple of
C implementations of Complementary-Multiply-With-Carry (CMWC) PRNGs,
which though not portable (IIRC) should be easy enough to adapt to
most C implementations. [snip]
On a related note: there was a thread on sci.crypt not long ago
regarding producing an unbiased subrange of the PRNG's range (a
common task which we were recently discussing here) while consuming
the minimum possible number of bits from the generator's output. In
other words, someone presented a more sophisticated and less wasteful
approach than just throwing away everything below the largest in-
range multiple of the desired range. [snip]
Ben Pfaff said:Or, if you want a solution in portable C, you can use mine:
http://www.stanford.edu/~blp/writings/clc/random.html
Eric Sosman said:Since the quality of rand() is implementation-specific,
"better" is hard to justify in a universal sense. Also, even
for a finite set of generators it is hard to justify "better"
across all possible problem spaces.
It seems that everybody[*] eventually latches onto a favorite
substitute for rand().
A few years ago it was proposed that
the C Standard should mandate the "Mersenne Twister" generator
(as it was before it was fixed ...). Now there's a fan base for
Knuth (in whatever version is now current; it, too, has been fixed)
and for Marsaglia (a name to reckon with in RNG's, although it's
not clear which of his many generators is today's favorite). My
own not-so-humble opinion is that the C Standard should leave the
requirements on rand() as weak as they are today, but should describe
it as a "coarse" source of variates, suitable for "casual" use but
not for serious work.
Eric said:The trouble is in the testing: If you sample three
hundred twenty-seven quadrillion rand() values and see
no zeroes, can you conclude that rand() will never, ever,
under any circumstances return a zero?
No, but there *are* statistical statements you *CAN* make. Either:
1. The calls to rand() are not independent
or
2. With a high degree of confidence you can say there is an
eccentricity in the distribution of the output probabilities that
underrepresents zero as an output.
This actually matters to some people, and its an interesting echo to
the "Can you use C for mathematical purposes?" thread from earlier that
nobody here seems to be sensitive to the above in any way.
Since the quality of rand() is implementation-specific,
"better" is hard to justify in a universal sense.
It seems that everybody[*] eventually latches onto a favorite
substitute for rand().
My own not-so-humble opinion is that the C Standard should leave the
requirements on rand() as weak as they are today, but should describe
it as a "coarse" source of variates, suitable for "casual" use but
not for serious work.
Alternatively, George Marsaglia has posted code here for a couple of
C implementations of Complementary-Multiply-With-Carry (CMWC) PRNGs,
which though not portable (IIRC) should be easy enough to adapt to
most C implementations. [snip]
The CMWC code that I was able to find (some of it in CLC postings)
usually used 'long long'.
By the way, I had trouble tracking down web articles on CMWC, because
most of the web writing on CMWC says complImentary rather than
complEmentary.
For practical purposes I'm inclined to think it's not worth the
trouble.
1. The calls to rand() are not independent
or
2. With a high degree of confidence you can say there is an
eccentricity in the distribution of the output probabilities that
underrepresents zero as an output.
This actually matters to some people, and its an interesting echo to
the "Can you use C for mathematical purposes?" thread from earlier that
nobody here seems to be sensitive to the above in any way.
Keith said:If an implementation's rand() function never returns 0, the implementation
is either non-conforming or of poor quality.
Do you know of an implementation in which rand() never returns 0?
I dare say that people who use C for mathematical purposes _are_ aware
of the above, and moreover are aware that
Anyone who considers arithmetical methods of producing
random digits is, of course, in a state of sin.
Alfred E. Neumann, 1951
Keith Thompson said:I dare say most of them are aware that the quotation is by John von
Neumann, not the guy on the cover of Mad Magazine.
(There is, of course, a connection between Mad Magazine and computer
science; google Potrzebie Knuth for details.)
Grumble said:Keith said:If an implementation's rand() function never returns 0, the
implementation is either non-conforming or of poor quality.
Do you know of an implementation in which rand() never returns 0?
I was under the impression that the PRNG discussed in [1] had been
used often in the old days?
[1] "Random number generators: good ones are hard to find"
http://www-scf.usc.edu/~csci105/links/p1192-park.pdf
u_0 = 1
u_{n+1} = 16807*u_n mod (2^31-1)
Come to think of it, an implementor could very well return u_n - 1
and set RAND_MAX to 2^31-3...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.