473,416 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

About rand()

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 ?

Jan 16 '07 #1
13 3635
Spiros Bousbouras wrote:
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.]

--
Peter

Jan 16 '07 #2
Peter Nilsson wrote:
Spiros Bousbouras wrote:
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 ?

Jan 16 '07 #3
In article <11**********************@v45g2000cwv.googlegroups .com>,
Spiros Bousbouras <sp****@gmail.comwrote:
>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.]

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Jan 16 '07 #4
Walter Roberson wrote:
In article <11**********************@v45g2000cwv.googlegroups .com>,
Spiros Bousbouras <sp****@gmail.comwrote:
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.
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.

Jan 16 '07 #5
In article <11**********************@51g2000cwl.googlegroups. com>,
Spiros Bousbouras <sp****@gmail.comwrote:
>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

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 16 '07 #6
Spiros Bousbouras wrote:
Walter Roberson wrote:
In article <11**********************@v45g2000cwv.googlegroups .com>,
Spiros Bousbouras <sp****@gmail.comwrote:
>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.

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.

Jan 16 '07 #7
"Spiros Bousbouras" <sp****@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
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_...tial_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.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 17 '07 #8
"David T. Ashley" wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Jan 17 '07 #9
"CBFalconer" <cb********@yahoo.comwrote in message
news:45***************@yahoo.com...
"David T. Ashley" wrote:
>>
... 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.
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.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 17 '07 #10
Walter Roberson wrote:
Spiros Bousbouras wrote:
>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.
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.
Jan 17 '07 #11
Spoon wrote:
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.
Jan 17 '07 #12
Spoon wrote:
Spoon wrote:
>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.
#include <stdlib.h>
int main(void)
{
while (rand() != 0);
return 0;
}

$ /usr/bin/time ./a.out
181.70user 0.03system 3:05.70elapsed 97%CPU

I stand corrected.

What is true is that linear congruential generators with B=0 obviously
never output 0.

http://en.wikipedia.org/wiki/Linear_...tial_generator
Jan 17 '07 #13

"Spiros Bousbouras" <sp****@gmail.comwrote in message
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;

Jan 18 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
3
by: Gunnar | last post by:
Hello. Problem: How can I select K random values from the elements 0,1,2,3,4...,N-1 ? I don't know if there's an easy way of doing this, but here are suggestions for doing it. One way is...
36
by: Profetas | last post by:
Hi, I want to generate a random 8 bit number using rand(0 is that possible? to expecifu the base and the lenght? thanks
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
2
by: xcm | last post by:
#include <stdlib.h> static unsigned long int next = 1; int rand(void) { next = next * 1103515245 + 12345; return (unsigned int)(next/(2 * (RAND_MAX +1L)) % (RAND_MAX+1L)); }
6
by: Roka | last post by:
Hi all. I'm reading a program which used the sentence below: #define NUM_THREADS 10 ... ... int rand_num; rand_num = 1+ (int) (9.0 * rand() / (RAND_MAX + 1.0)); sleep(rand_num); ... ......
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
5
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
10
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.