473,396 Members | 1,779 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,396 software developers and data experts.

Quality of rand()

Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness. Can anyone
suggest a test I could use that it would fail? A pointer to a suitable
article would be appreciated.

Thanks for your help in advance,

Tom

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #1
11 4177
"Tom McCallum" <te********@hotmail.com> wrote in message
news:op**************@news.blueyonder.co.uk...
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness. Can anyone
suggest a test I could use that it would fail? A pointer to a suitable
article would be appreciated.

You cannot make a general statement about the quality of rand, as its actual
performance and properties will vary from one library implementation to the
other.
But as a rule of thumb, rand() is designed to be fast and uses a simple
Linear Congruential Generator. Historically, there has also been many
complaints that the LCG constants used by some implementations were poor.
So the real problem is: you cannot rely on rand() producing good quality
random numbers.

For an intro to LCG and other random number generators, see for example
http://www.taygeta.com/rwalks/node1.html, or read Knuth's TAOCP
http://www-cs-faculty.stanford.edu/~knuth/taocp.html.

Games and many casual applications can do with a 'predictable'
random number generator (RNG), but many security-sensitive
applications (network protocols, encryption) require truly
random sources. I'm not familiar with the NIST suite you used,
does it look for things such as attractors and bits of randomness?
The following article may provide an interesting illustration
of tests that evaluate the security weaknesses of a RNGs:
http://lcamtuf.coredump.cx/newtcp/

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #2
On Wed, 18 Aug 2004 11:28:20 +0200, Ivan Vecerina
<NO**********************************@vecerina.com > wrote:
"Tom McCallum" <te********@hotmail.com> wrote in message
news:op**************@news.blueyonder.co.uk...
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the
outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness. Can anyone
suggest a test I could use that it would fail? A pointer to a suitable
article would be appreciated.

You cannot make a general statement about the quality of rand, as its
actual
performance and properties will vary from one library implementation to
the
other.
But as a rule of thumb, rand() is designed to be fast and uses a simple
Linear Congruential Generator. Historically, there has also been many
complaints that the LCG constants used by some implementations were poor.
So the real problem is: you cannot rely on rand() producing good quality
random numbers.

For an intro to LCG and other random number generators, see for example
http://www.taygeta.com/rwalks/node1.html, or read Knuth's TAOCP
http://www-cs-faculty.stanford.edu/~knuth/taocp.html.

Games and many casual applications can do with a 'predictable'
random number generator (RNG), but many security-sensitive
applications (network protocols, encryption) require truly
random sources. I'm not familiar with the NIST suite you used,
does it look for things such as attractors and bits of randomness?
The following article may provide an interesting illustration
of tests that evaluate the security weaknesses of a RNGs:
http://lcamtuf.coredump.cx/newtcp/

hth -Ivan


Thanks for your reply, in answer to your question the NIST suite uses the
following tests:
[01] Frequency [02] Block Frequency
[03] Cumulative Sums [04] Runs
[05] Longest Run of Ones [06] Rank
[07] Discrete Fourier Transform [08] Nonperiodic Template
Matchings
[09] Overlapping Template Matchings [10] Universal Statistical
[11] Approximate Entropy [12] Random Excursions
[13] Random Excursions Variant [14] Serial
[15] Lempel-Ziv Complexity [16] Linear Complexity

I am not sure if these cover 'attractors and bits of randomness' but as
far as I can tell they seem to be a reasonable collection.

Cheers

Tom

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #3
Tom McCallum wrote:
Can any tell me why rand() is such a bad pseudo-random number
generator. In all the articles I have read they say that you can
predict the outcome of rand() but when I used its output with NIST's
random number testsuite


You are mixing up two different things. The outcome of rand() is
predictable for the sheer reason, that it is implementing an
deterministic algorithm. From a given starting configuration (which may
be unknown) it generates number that look random and pass the tests.
Knowing the algorithm, it can be concluded from a couple of given
values, what will follow, but the property 'how hard is it to predict
the next number if you consider that rand() is deterministic' is
nothing that a randomness test checks (and it would be too hard to be
done, btw.), because it doesn't play a role for the usual purposes of a
random generator, the numbers alone should look random, that's enough.

The notable exception is cryptography, where "real" randomness is a
security issue.

Marco

Jul 22 '05 #4

"Tom McCallum" <te********@hotmail.com> wrote in message
news:op**************@news.blueyonder.co.uk...
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness.


This is curious. I tested rand()'s(gcc 3.2) output some time back using the
Diehard battery of tests, and it failed miserably, giving p-values of 1.0000
for loads of the tests. Can you show me the program you used to write your
random data and tell me what compiler you are using? Thanks, Joe
Jul 22 '05 #5
Tom McCallum writes:
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness. Can anyone
suggest a test I could use that it would fail? A pointer to a suitable
article would be appreciated.


The problem is that rand it not an algorithm, it is the name of a function.
Writers who say such things are showing off how erudite they are, a bit like
correcting soneone when someone say "random number generator" and the
sophisticated person responds, "Of course this uninformed doofus actually
means *pseudo* random number generator.

Nowhere is it written that rand() must be bad, or is necessarily bad or that
any of the current generators are necessarily bad. What they really mean
is:"Once upon a time there was a bad implementation of a PRNG that was
called by rand(). Being able to predict the output of a generator is -
given sufficient effort - , a property of *all* PRNGs, it goes with the
territory.
Jul 22 '05 #6
Joe C wrote:

"Tom McCallum" <te********@hotmail.com> wrote in message
news:op**************@news.blueyonder.co.uk...
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness.


This is curious.


Not at all. The standard doesn't dictate which formula to use for rand()
There are good ones and there are bad ones.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7
"Tom McCallum" <te********@hotmail.com> wrote in message
news:op**************@news.blueyonder.co.uk...
Thanks for your reply, in answer to your question the NIST suite uses the
following tests: .... I am not sure if these cover 'attractors and bits of randomness' but as
far as I can tell they seem to be a reasonable collection.


If the rand() implementation you are using passes the NIST tests,
it is likely to be reasonably good for many casual applications.
But the scope of these tests is quite limited (and on another platform,
rand() may not perform as well).

I really think that you should read the TCP/IP spoofing article I sent a
link to, or even better, the original article it refers to:
http://minilien.com/?LtytjTcByE (.pdf, describes the approach in detail)
To summarize things: the plots are basically the position of points whose
coordinates are generated from consecutive values obtained from a "random"
source. A truly random source would generate a uniform cloud of points. When
points tend to concentrate in some areas ("attractors"), it is obvious that
the data source is not as random as it would seem: the attractors allow you
to make a guess of the next value if the previous sequence is similar to a
previously encountered one. For a given analysis technique applied to a
source stream of data, the actual bits of randomness are defined by your
probability of making a correct guess.

Going back to your original question and rand() itself, let's see how easy
it would be to predict the output of rand():
First of all, unless you call srand(), you will notice that the sequence of
values returned by rand() is always the same -- and this is a requirement of
the ISO C/C++ standard.
If you do call srand() with a given value, the resulting sequence is
required to always be the same. So the set of possible sequences is limited
to the set of values your program may pass to srand().
Now let's assume you are actually calling srand() with a truly random 32-bit
value:
On a cheap hard disk, with access to your library implementation, it might
take me less than an hour to store a look-up table that matches all the
initial value(s) returned by rand() to a seed value passed to srand().
Given the first value(s) returned by rand(), I could then instantly compute
the seed and generate/predict the rest of the random sequence.

So would you rely on rand() to implement any security-sensitive application
?

Cheers, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #8
[snips]

osmium wrote:
Nowhere is it written that rand() must be bad, or is necessarily bad or that
any of the current generators are necessarily bad. What they really mean
is:"Once upon a time there was a bad implementation of a PRNG that was
called by rand().


Not quite. The issue is that since there are no guarantees about the
performance or characteristics of rand(), an implementation can get away
with very poor quality PRNGs if it wants - and some have. As a result
of this lack of guarantees, one cannot simultaneously assume that a)
rand() will be good and b) code relying on rand() being good will be
readily usable under other implementations.

Net result: if it's okay to possibly wind up with a poor PRNG, go ahead
and use rand(). If you need something that is going to produce better
results, don't rely on rand(), use something else.
Jul 22 '05 #9
"Kelsey Bjarnason" <ke*****@xxnospamyy.lightspeed.bc.ca> wrote in message
news:41******@rsl2.rslnet.net...

Not quite. The issue is that since there are no guarantees about the
performance or characteristics of rand(), an implementation can get away
with very poor quality PRNGs if it wants - and some have. As a result
of this lack of guarantees, one cannot simultaneously assume that a)
rand() will be good and b) code relying on rand() being good will be
readily usable under other implementations.

Net result: if it's okay to possibly wind up with a poor PRNG, go ahead
and use rand(). If you need something that is going to produce better
results, don't rely on rand(), use something else.


One of the problems with the standard implementation of rand() is that on
most systems it uses a fast formula called Linear Congruential. This
formula, when coupled with a time seed, produces a sequential number as it's
first output after seeding. On my system, the first number generated after
seeding steps by three once a second. This problem relates to time(NULL)
being a number that doesn't change much.

So if you use rand(), and srand( time(NULL) ), then it's a good idea to toss
the first result.

--
AngleWyrm
The C++ hat random container
http://home.comcast.net/~anglewyrm/hat.html
Jul 22 '05 #10
AngleWyrm writes:
One of the problems with the standard implementation of rand() is that on
most systems it uses a fast formula called Linear Congruential.


But there IS NO STANDARD implementation of rand(). It is the name of a
FUNCTION, not an ALGORITHM!!!

Jul 22 '05 #11
"osmium" <r1********@comcast.net> wrote in message
news:2o************@uni-berlin.de...
AngleWyrm writes:
One of the problems with the standard implementation of rand() is that on most systems it uses a fast formula called Linear Congruential.


But there IS NO STANDARD implementation of rand(). It is the name of a
FUNCTION, not an ALGORITHM!!!


You don't program much, do you?

http://home.comcast.net/~anglewyrm/hat.html
Jul 22 '05 #12

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

Similar topics

7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
36
by: Ben Justice | last post by:
For a program in c, I need some random numbers for a system were people are placing bets. This is not a commerical project btw. Generally, I tend to rely on things from the standard library,...
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
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
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...
13
by: Spiros Bousbouras | last post by:
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...
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))
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.