True Random numbers

T

TreatmentPlant

I need to generate a few thousand true random numbers using C++.

I have some code now that does alright, but when you plot the results on
a graph, you notice patterns, so the numbers are not truly random, but
pseudo-random.

I tried randomly seeding the seed! to see if that improved things; it
didn't.

Can anyone help?

--8<-------------------------
float l_fMin=0.0;
float l_fMax=99.999;
int newSeed;
float num;

srand((unsigned)time(NULL));

for(int i=0;i<=2000;i++)
{
newSeed=rand();
srand((unsigned)newSeed);
num = ((float)rand() / (RAND_MAX+1)) * ((float)rand() / (RAND_MAX+1));

pfRandom = (float)((num * (l_fMax - l_fMin)) + l_fMin);
}
 
A

Alf P. Steinbach

* TreatmentPlant:
I need to generate a few thousand true random numbers using C++.

I have some code now that does alright, but when you plot the results on
a graph, you notice patterns, so the numbers are not truly random, but
pseudo-random.

I tried randomly seeding the seed! to see if that improved things; it
didn't.

Can anyone help?

This is partially or mostly off-topic.

But since C++ implementation suffer from bad random number generators
just like other languages:

Donald Knuth, in "The Art of Computer Programming", discussed how most
random generators at that time (early 1970's) were flawed, just blindly
copied implementations relying on lore and misconceptions, and how to
generate nearly truly random numbers from such a flawed random number
generator by using an intermediate buffer providing more history.

That approach might be described as heaping complexity on complexity,
but works.

Stephen Wolfram, in "A New Kind of Science", discussed how to generate
nearly truly random numbers from a /very/ simple cellular automation.
Not surprisingly, that's the generator used in Mathematica. This
approach might be described as reduction to the absolutely simplest
possible, but that doesn't mean it's easier to analyze: amazingly, it
seems that any statistically "perfect" random number generator must be
taken on faith, for the very feature that makes it so perfectly random
also makes it impossible or near impossible to analyze.


Follow-ups to [comp.programming].
 
R

Roy Smith

TreatmentPlant said:
I need to generate a few thousand true random numbers using C++.

I have some code now that does alright, but when you plot the results on
a graph, you notice patterns, so the numbers are not truly random, but
pseudo-random.

This is very much not a C++ question. Use of random number generators
is independant of the host language. In any case, the obvious thing I
notice is that you're using rand(). On most systems, random()
provides a much better PRNG (pseudo-random number generator) than
rand().

Beyond that, you really need to do some reading about statistics to
understand if random() is good enough for your purposes. No matter
how random the number sequence is, you'll always find *some* patterns.
For example, if you pick enough random numbers, you will occassionally
find 4 in a row which are increasing in value. It takes a fairly deep
understanding of statistics to figure out if such patterns are
significant or not.

Even random() generates "pseudo-random" numbers. I'm not sure exactly
what you mean by "truly random", but if you want to avoid PRNGs, you
need to base your random number generator on some physical process,
such as measuring the event intervals of radioactive decay, or photon
arrival times. Many systems have an "entropy store", which attempts
to measure physical events like keystroke intervals and disk head seek
times to get better randomness.

If you're dealing cards from a deck for a game of on-line poker,
random() should be good enough. If you're building a cryptographic
system, it may not be. It's not an easy question to answer.
 
M

Markus Schoder

TreatmentPlant said:
I need to generate a few thousand true random numbers using C++.

I have some code now that does alright, but when you plot the results on
a graph, you notice patterns, so the numbers are not truly random, but
pseudo-random.

I tried randomly seeding the seed! to see if that improved things; it
didn't.

Try boost -- it implements a vast collection of high quality pseudo
random number generators. Of particular note is the mt19937 generator
(period length 2^19937-1) with good distribution quality up to 623
dimensions and also reasonably good performance.
 
O

osmium

:

I need to generate a few thousand true random numbers using C++.

I have some code now that does alright, but when you plot the results on a
graph, you notice patterns, so the numbers are not truly random, but
pseudo-random.

I would try using a file such as this. I think this is a continuation of
the work at Rand, you could probably spend a day or so trying to track down
the original source of the data. I have never seen complaints from a
respected person that the original list(_A Million Random Digits_ )was
faulty.

http://www.pisquaredoversix.force9.co.uk/FiveMillionRandomDigits.txt

I remember downloading several (ten or so) similar - but different - files
several years ago to keep for when I needed them. Of course I have no way
to find them now.
 
T

TreatmentPlant

Roy Smith wrote:
No matter how random the number sequence is, you'll always find *some* patterns.
For example, if you pick enough random numbers, you will occassionally
find 4 in a row which are increasing in value. It takes a fairly deep
understanding of statistics to figure out if such patterns are
significant or not.
<snip>

By patterns I meant that after about 250 or so points, the sequence will
repeat itself. If I run the app several times, over and over and over,
I can get the "pattern" appearing as often as every 50 or so points (I
haven't counted them exactly)
 
J

Jerry Coffin

01.iinet.net.au>, (e-mail address removed)
says...
I need to generate a few thousand true random numbers using C++.

"Anyone who considers arithmetical methods of producing
random digits is, of course, in a state of sin." (John
Von Neumann).

If you really want truly random numbers, you'll need to
look at special hardware. The most common relies on a
small chip of mildly radioactive material (usually
Americurium from a smoke detector). Googling in the
archives of sci.crypt should turn up pointers to a number
of sources.

If you're willing to settle for pseudo-random numbers,
and just want a higher quality generator than your
standard library provides, quite a few are easily
available. Googling for something like "diehard pseudo
random" should yield a fair number of hits. Diehard is a
test suite for random number generators; somebody who
mentions it is probably saying that their generator has
passed its tests, which tends to indicate better quality
than most. Alternatively, Knuth volume 2 has a fairly
extensive chapter on PRNGs, if you want to design your
own.
 
O

Old Wolf

Roy said:
If you're dealing cards from a deck for a game of on-line poker,
random() should be good enough. If you're building a cryptographic
system, it may not be. It's not an easy question to answer.

Not quite sure you have your priorities straight there :)
Any non-randomness in a poker RNG could translate into millions
of dollars of difference. Try reading rec.gambling.poker for a day
and look at all the threads discussing RNGs.

To the OP: you might like to try downloading (or writing your own)
better
PRNG. See

http://en.wikipedia.org/wiki/PRNG#Mersenne_twister

for a relatively easy one to code. If this isn't good enough
then you could step up to a CSRNG (see the following section).
You can use OpenSSL as a library with canned cryptographic
functions (and its own RNG).
 
N

Nick Keighley

TreatmentPlant said:
I need to generate a few thousand true random numbers using C++.

I have some code now that does alright, but when you plot the results on
a graph, you notice patterns, so the numbers are not truly random, but
pseudo-random.

I tried randomly seeding the seed! to see if that improved things; it
didn't.

Can anyone help?

--8<-------------------------

minor changes to code layout
float l_fMin=0.0;
float l_fMax=99.999;
int newSeed;
float num;

srand((unsigned)time(NULL));

for(int i=0;i<=2000;i++)
{
newSeed=rand();
srand((unsigned)newSeed);

don't do this! Only seed the rng ONCE in a program run
num = ((float)rand() / (RAND_MAX+1)) * ((float)rand() / (RAND_MAX+1));

pfRandom = (float)((num * (l_fMax - l_fMin)) + l_fMin);
}
 
R

Roy Smith

"Old Wolf said:
Not quite sure you have your priorities straight there :)
Any non-randomness in a poker RNG could translate into millions
of dollars of difference. Try reading rec.gambling.poker for a day
and look at all the threads discussing RNGs.

You may be right! How about generating initial setups for a game of "Hunt
the Wumpus"?
 
M

Martin Aupperle

Not quite sure you have your priorities straight there :)
Any non-randomness in a poker RNG could translate into millions
of dollars of difference. Try reading rec.gambling.poker for a day
and look at all the threads discussing RNGs.
Not really, because shuffling a deck of physical cards is by far not
random. Not all cards are equal! Same as roulette in a real casino.
Different story with internet gambling, though.

With internet poker you have to take the algorithms of the virtual
casinos into account. Try Black Jack, much easier.
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top