469,890 Members | 1,425 Online

# True Random numbers

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[i] = (float)((num * (l_fMax - l_fMin)) + l_fMin);
}
Apr 24 '06 #1
11 12066
* 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].

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 24 '06 #2
TreatmentPlant <tr************@DIESPAMMERS.iinet.net.au> wrote:
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.
Apr 24 '06 #3
TreatmentPlant wrote:
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.

Apr 24 '06 #4
"TreatmentPlant" writes:

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.c...ndomDigits.txt

several years ago to keep for when I needed them. Of course I have no way
to find them now.
Apr 24 '06 #5
Roy Smith wrote:
<snip>
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)
Apr 24 '06 #6
Maybe this is useful:
http://www.random.org/

Apr 25 '06 #7
01.iinet.net.au>, tr************@DIESPAMMERS.iinet.net.au
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 26 '06 #8
Roy Smith wrote:
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.

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).

Apr 27 '06 #9
TreatmentPlant wrote:
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[i] = (float)((num * (l_fMax - l_fMin)) + l_fMin);
}

--
Nick Keighley

Apr 27 '06 #10
"Old Wolf" <ol*****@inspire.net.nz> wrote:
Roy Smith wrote:
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.

You may be right! How about generating initial setups for a game of "Hunt
the Wumpus"?
Apr 27 '06 #11
On 26 Apr 2006 20:48:29 -0700, "Old Wolf" <ol*****@inspire.net.nz>
wrote:

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.
Apr 28 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.