About rand()

S

Spiros Bousbouras

The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?

On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
 
P

Peter Nilsson

Spiros said:
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?

On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?

It's entirely a QoI issue. Pragmatically, it is trivial to write a
rand()
that returns every value at some point, even if the distribution is
not perfect. [A few implementations I've seen copy the example
shown in K&R2.]

The FAQ has a number of comments on the issues of rand().

Anyone needing to use random numbers in a serious program will
likely roll their own routine from the plethora of PRNG's available on
the net. [E.g. http://www.stanford.edu/~blp/writings/clc/random.html.]
 
S

Spiros Bousbouras

Peter said:
Spiros said:
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?

On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?

It's entirely a QoI issue. Pragmatically, it is trivial to write a
rand()
that returns every value at some point, even if the distribution is
not perfect. [A few implementations I've seen copy the example
shown in K&R2.]

By "perfect" distribution do you mean uniform distribution ?
 
W

Walter Roberson

The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?

It must produce a pseudo-random sequence in the range 0
to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
implementation it can never produce RAND_MAX then RAND_MAX
for that implementation would be defined as the largest value that
it -could- produce, and if that value was not at least 32767 then
the implementation would be non-conforming.

One could similarily argue that if 0 cannot be produced that
the implementation is non-conforming; the argument is perhaps
a bit weaker.

If the implementation alternated between 0 and RAND_MAX then
it would be conforming.

On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?

I would not -expect- any self-respecting rand() to not be
able to produce one of the values in the range, eventually;
I would -expect- at worst the sample function given in the
standard. But it wouldn't shock me if some organization
that produced a C-like language used what they -thought-
was a good rand() but which turned out not to be able to produce
some set of values. [NB: the number of organizations that
produce C-like languages appears to far outnumber the ones that
produce conforming C.]
 
S

Spiros Bousbouras

Walter said:
It must produce a pseudo-random sequence in the range 0
to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
implementation it can never produce RAND_MAX then RAND_MAX
for that implementation would be defined as the largest value that
it -could- produce, and if that value was not at least 32767 then
the implementation would be non-conforming.

The standard says "The rand function computes a
sequence of pseudo-random numbers in the range
of 0 to RAND_MAX." I don't see how it follows from
that that the largest value which can be produced
will by definition be RAND_MAX.
 
R

Richard Tobin

Spiros Bousbouras said:
The standard says "The rand function computes a
sequence of pseudo-random numbers in the range
of 0 to RAND_MAX." I don't see how it follows from
that that the largest value which can be produced
will by definition be RAND_MAX.

The standard is rather weak in its description of rand(). It makes
no mention of the distribution of random numbers, so an implementation
that returns 1 99.99999999% of the time could still be conforming.
Presumably this would be considered a quality of implementation issue,
and the same goes for an implementation that never produced RAND_MAX.

-- Richard
 
U

user923005

Spiros said:
The standard says "The rand function computes a
sequence of pseudo-random numbers in the range
of 0 to RAND_MAX." I don't see how it follows from
that that the largest value which can be produced
will by definition be RAND_MAX.

According to that definition the sequence:
1,1,1,1...
seems to fit because 1 is between 0 and RAND_MAX.

The rand() function cannot produce negative numbers (by definition)
The rand() function cannot produce numbers larger than RAND_MAX (by
definition).

Generally speaking, a halfway-decent rand() implementation will
eventually produce every number between and including 0 .. RAND_MAX.

However, there is not guarantee in the standard that rand() is halfway
decent (e.g. that it passes Marsaglia's tests).

If you want good pseudo-random numbers, then use the Mersenne Twister.
 
D

David T. Ashley

Spiros Bousbouras said:
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?

On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?

Pseudo-random means that the next value of the output is a function of some
internal state. Often, the state and the returned value are the same thing.
Pseudo-random means that the next value is algorithmically predictable, i.e.
it isn't truly random where one could not predict.

http://en.wikipedia.org/wiki/Pseudo-random

The most common approach to pseudo-random generators is to use prime moduli.

http://en.wikipedia.org/wiki/Linear_congruential_generator

In fact, most people who implement their own random number generators are
pretty happy with the simple one at the link immediately above. I believe
as long as the requirement of coprimality between a couple of the parameters
is met, the generated sequence is guaranteed to hit every value in the
interval. I'm sure all the math is at the link above.
 
C

CBFalconer

David T. Ashley said:
.... snip ...

In fact, most people who implement their own random number
generators are pretty happy with the simple one at the link
immediately above. I believe as long as the requirement of
coprimality between a couple of the parameters is met, the
generated sequence is guaranteed to hit every value in the
interval. I'm sure all the math is at the link above.

Just read Knuth (TAOCP). He has a thorough treatment of linear
congruential generators, including some cookbook tables.
 
D

David T. Ashley

CBFalconer said:
Just read Knuth (TAOCP). He has a thorough treatment of linear
congruential generators, including some cookbook tables.

Thanks. I have those books on my shelves and actually didn't know that
random number generation was in it (I've used it most for extended-precision
arithmetic and searching and sorting).

I've always admired Knuth. He has a mathematician's gift for brevity.
There is a lot packed into those books.
 
S

Spoon

Walter said:
It must produce a pseudo-random sequence in the range 0
to RAND_MAX, where RAND_MAX is at least 32767. If on a particular
implementation it can never produce RAND_MAX then RAND_MAX
for that implementation would be defined as the largest value that
it -could- produce, and if that value was not at least 32767 then
the implementation would be non-conforming.

One could similarily argue that if 0 cannot be produced that
the implementation is non-conforming; the argument is perhaps
a bit weaker.

The sequence
u{n+1} = M * u{n} MOD C
with M=16807 and C=2^31-1
is often used as a PRNG.

It does not produce 0. (Otherwise it would always return 0.)

glibc's rand() implementation does not produce 0.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long i = 1;
for (i=1; i; ++i)
{
if (rand() == 0) puts("rand() == 0");
}
return 0;
}

$ /usr/bin/time ./a.out
161.65user 0.08system 2:56.68elapsed 91%CPU

If the implementation alternated between 0 and RAND_MAX then
it would be conforming.

On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?

I would not -expect- any self-respecting rand() to not be
able to produce one of the values in the range, eventually;
I would -expect- at worst the sample function given in the
standard. But it wouldn't shock me if some organization
that produced a C-like language used what they -thought-
was a good rand() but which turned out not to be able to produce
some set of values. [NB: the number of organizations that
produce C-like languages appears to far outnumber the ones that
produce conforming C.]

Regards.
 
S

Spoon

Spoon said:
glibc's rand() implementation does not produce 0.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long i = 1;
for (i=1; i; ++i)
{
if (rand() == 0) puts("rand() == 0");
}
return 0;
}

$ /usr/bin/time ./a.out
161.65user 0.08system 2:56.68elapsed 91%CPU

Doh! This only means that glibc's rand() does not return 0 after 2^32
invocations. Not that it never returns 0.
 
M

Malcolm McLean

Spiros Bousbouras said:
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
Pseudorandom means that it passes or nearly passes a lot of tests for
randomness, but is in fact deterministic.
When you say "would a rand() that always returned the same number/
alternated between 2 values be conforming?" really you are asking a
meaningless question. It just depends on the precise English-language
sentence used in the standard. There are no requirements for RNGs other than
that they be RNGs.
On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
[0,RAND_MAX] will be returned ?
A decent implementation will do this, but you cannot rely on it. The numbers
will be very roughly uniformly distributed throughout the range, but the
lower bits are allowed to cycle.
Hence

x * rand()/(RAND_MAX + 1.0)

rather than

rand() % x;
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top