31

I recently saw this question over at math.SE. It got me thinking. Could Pi be used as a crude random number generator? I mean the results are well known(how long has pi been computed to now?) but, Pi does seem to be quite random when taken 1 digit at a time.

Does this make any sense at all?

Earlz
  • 22,658
  • 7
  • 46
  • 60
  • Where are these random numbers going to be used? – NullUserException Oct 19 '12 at 16:58
  • 2
    Theoretically it could be but it would probably be less optimal than the current methods. Just instincts on that but it seems the random pool is bigger in this way with less overhead. – Rig Oct 19 '12 at 16:58
  • @NullUserException Not sure... I was just wondering if they could be used AT ALL. I assume this would definitely not be for cryptography though' – Earlz Oct 19 '12 at 16:59
  • Are you suggesting computing the nth digit of pi for a random number? or are you thinking of storing a few megabytes of digits of pi and using that? Either way - you can... is it a good idea? probably not. –  Oct 19 '12 at 17:10
  • in generators, important thing is getting a good approximation of even distribution, I wouldn't bet that Pi has this – gnat Oct 19 '12 at 17:15
  • Try tossing the digits of pi into http://www.fourmilab.ch/random/ - it would take some amount of coercion to get the decimal digits into an appropriate stream of bits/bytes. –  Oct 19 '12 at 17:22
  • As a *crude* rng, yes it will probably work (then again, almost anything would work for low enough values of "crude"). As a *good* rng, I'm not as sure... – FrustratedWithFormsDesigner Oct 19 '12 at 17:48
  • The randomness of pi numbers is used in a method to compute pi with itself. This is not the fastest algorithm, but I find that quite poetic :-) – Simon Bergot Oct 19 '12 at 17:50
  • @Simon: Really? Do you have a link to that? – FrustratedWithFormsDesigner Oct 19 '12 at 18:28
  • 3
    @FrustratedWithFormsDesigner - its part of the ent package. It uses the random numbers to calculate the area of a circle inscribed within a square and from that, one can calculate pi. Using the bits of pi as the random numbers, there is a certain elegance to use that data to compute pi. –  Oct 19 '12 at 18:30
  • If you pick digits from it at random, yes. ;) – Paul Butcher Oct 19 '12 at 19:10
  • Wouldn't you have to generate a random number in order to select a number from the decimal part od pi ? – Tulains Córdova Oct 19 '12 at 19:23
  • @MichaelT: "ent package"? what is this? – FrustratedWithFormsDesigner Oct 19 '12 at 19:26
  • 1
    @FrustratedWithFormsDesigner [ent](http://www.fourmilab.ch/random/) is a suite of code for analyzing the pseudo randomness of bunch of bytes. One test within it is a Monte Carlo for calculating pi and comparing the random calculation against the actual value to see how random it is. –  Oct 19 '12 at 19:31
  • Depending on what you are doing, but I think you can use the decimals of the square root of any prime number as a random number generator. These should at least have evenly distributed digits. – Per Alexandersson Dec 31 '12 at 15:51
  • @Paxinum I've looked at the sqrt(2), and it does show many of the same "its random" properties. –  Jan 02 '13 at 16:52
  • You can, but you shouldn't, there is faster methods to produce harder to predict sequences. Also it's not really about π, same properties can be observed in any irrational numbers ( http://en.wikipedia.org/wiki/Irrational_number ) – David Sergey Mar 21 '14 at 09:11

4 Answers4

51

Digging from http://www.befria.nu/elias/pi/binpi.html to get the binary value of pi (so that it was easier to convert into bytes rather than trying to use decimal digits) and then running it through ent I get the following for an analysis of the random distribution of the bytes:

Entropy = 7.954093 bits per byte.

Optimum compression would reduce the size of this 4096 byte file by 0 percent.

Chi square distribution for 4096 samples is 253.00, and randomly would exceed this value 52.36 percent of the times.

Arithmetic mean value of data bytes is 126.6736 (127.5 = random).

Monte Carlo value for Pi is 3.120234604 (error 0.68 percent).

Serial correlation coefficient is 0.028195 (totally uncorrelated = 0.0).

So yes, using pi for random data would give you fairly random data... realizing that it is well known random data.


From a comment above...

Depending on what you are doing, but I think you can use the decimals of the square root of any prime number as a random number generator. These should at least have evenly distributed digits. – Paxinum

So, I computed the square root of 2 in binary to undetake the same set of problems. Using Wolfram's Iteration I wrote a simple perl script

#!/usr/bin/perl
use strict;
use Math::BigInt;

my $u = Math::BigInt->new("2");
my $v = Math::BigInt->new("0");
my $i = 0;

while(1) {
    my $unew;
    my $vnew;

    if($u->bcmp($v) != 1) { # $u <= $v
        $unew = $u->bmul(4);
        $vnew = $v->bmul(2);
    } else {
        $unew = ($u->bsub($v)->bsub(1))->bmul(4);
        $vnew = ($v->badd(2))->bmul(2);
    }   

    $v = $vnew;
    $u = $unew;

    #print $i,"  ",$v,"\n";
    if($i++ > 10000) { last; }
}

open (BITS,"> bits.txt");
print BITS $v->as_bin();
close(BITS);

Running this for the first 10 matched A095804 so I was confident I had the sequence. The value vn as when written in binary with the binary point placed after the first digit gives an approximation of the square root of 2.

Using ent against this binary data produces:

Entropy = 7.840501 bits per byte.

Optimum compression would reduce the size
of this 1251 byte file by 1 percent.

Chi square distribution for 1251 samples is 277.84, and randomly
would exceed this value 15.58 percent of the times.

Arithmetic mean value of data bytes is 130.0616 (127.5 = random).
Monte Carlo value for Pi is 3.153846154 (error 0.39 percent).
Serial correlation coefficient is -0.045767 (totally uncorrelated = 0.0).
  • Exactly the type of answer I was looking for. I have no idea how to calculate all of this type of stuff – Earlz Oct 19 '12 at 18:14
  • Even if the number distribution is fairly random, don't you have to find a way to *randomly* select a portion of it? – Blumer Oct 19 '12 at 22:56
  • 1
    @Blumer no. Randomness is measured on a sequence of numbers. The sequence of pi digits is said to be random. See http://en.wikipedia.org/wiki/Statistical_randomness – Simon Bergot Oct 20 '12 at 10:28
  • 12
    Absolutely right. And because it is well known random data, don't you ever dare to use that for cryptographic purposes. – Falcon Jan 01 '13 at 11:18
  • 3
    +1 for "well known random data". If you need random data that someone can't guess, pi is not for you, if just need a bunch of random numebers for some reason, it works just fine. – jmoreno Mar 21 '14 at 02:34
  • @Falcon the same could be said of other CSPRNGs, but we can seed a pi-based PRNG by skipping the first `s` digits. Provided that the seed is sufficiently large and random, the sequence being well-known wouldn't make the generator easier to predict. – Daniel Lubarov Mar 21 '14 at 05:43
5

Well, among other properties of a random number generator, you probably want it to be a normal number. And several answers in the math.SE question that inspired your question point out that currently pi is believed to be normal, but it has not been proven.

psr
  • 12,846
  • 5
  • 39
  • 67
2

Such generator would be a pseudo number generator, i.e. given the same seed, the result would always be the same. This being said, in most frameworks, when you use the standard random number generator, there is the same issue of being pseudo-random.

The distribution of the digits seems to be quite similar to the standard random number generators¹, so the digits of π can be used for ordinary random number generation scenarios.

The issue is that the algorithm will probably be very slow, compared to the ordinary random number generators, so it's not very useful in practice.


¹ I believe it's true, but don't have any proof. It would be interesting (and not to complicate) to do a comparison based on a large quantity of numbers.

Arseni Mourzenko
  • 134,780
  • 31
  • 343
  • 513
  • Correct me if I'm wrong, but *all* random number generators are pseudo-random, owing to the fact that computers are deterministic. The issue really is to get enough true randomness to seed a PRNG and have said PRNG be as unpredictable as possible. – NullUserException Oct 19 '12 at 17:26
  • 5
    @NullUserException: No, some random number generators use a source of entropy. This can be done either via specialized hardware (the approach taken by http://www.random.org) or by using existing sources of entropy (measurable fluctuations within existing hardware sensors, certain types of user interactions, micro-variations in certain types of performance tests, etc.). – Brian Oct 19 '12 at 17:41
  • 1
    @NullUserException: there are cryptographically secure PRNG, which are still pseudo-random. Then there are real RNG which are based on input from the real world: radioactive decay, noise, etc. – Arseni Mourzenko Oct 19 '12 at 17:42
  • @Brian A bit of a wording fail on my part, but I'm aware of dedicated hardware and things like `/dev/random`. But (if I am not mistaken) when you have to generate large amounts of random numbers, you still need a PRNG since real entropy is hard to come by. That's why I said *"get enough **true** randomness to seed a PRNG"* – NullUserException Oct 19 '12 at 18:43
  • 2
    @MainMa But even then, the randomness of radioactive decay, atmospheric noise, derived from user input, etc. is debatable. Just because we don't recognize a pattern doesn't mean one doesn't exist. – NullUserException Oct 19 '12 at 18:44
  • @NullUserException: it's not a question of recognizing patterns, but rather to predict the result from the input. You can't possibly predict radioactive decay or atmospheric noise in practice, so you can assume that for humans, it's truly random. – Arseni Mourzenko Oct 19 '12 at 19:05
  • 1
    @NullUserException: Last year Colbeck/Renner published a paper which purports to prove: "No extension of quantum theory can have improved predictive power." Assuming this holds up, there may be a source of entropy which is truly unpredictable, rather than merely infeasible to predict. – Brian Oct 19 '12 at 19:07
  • @Brian how about a [lava lamp](http://en.wikipedia.org/wiki/Lavarand)? –  Oct 19 '12 at 19:17
  • 1
    @MainMa - you would still perform mathematical tests for randomness. Even though the underlying physics is random (to the best of our knowledge) it doesn't mean that measurement is. Detectors of all types have a lot of 'interesting' behaviour in the real world – Martin Beckett Oct 21 '12 at 17:03
  • @MichaelT the most surprising thing about that is someone got a patent on taking pictures of lava lamps – Earlz Jan 02 '13 at 17:05
  • @Earlz it is a novel and non-obvious approach to generating a random number seed. It is standard practice in tech companies that if someone does something interesting while on work time, the legal department will see if they can get a patent for it (no matter how useful it is). It has been cited a number of times (showing it does have some use) and it isn't an overly broad patent that is preventing other companies from doing basic things. –  Jan 02 '13 at 17:23
2

The randomness of digits of pi (or for that matter any other sequence) can be arguably tested by so-called 'battery tests'. One popular battery test is George Marsaglia's Diehard Battery Test. There is also NIST Special publication 800-22 that describes a number of such tests and the results of applying these tests to a number of physical constants, including - lo and behold - pi for over a million bits. The result of pi is given in Appendix B of the report and looks like this:

Statistical Test                            P-value
Frequency                                   0.578211
Block Frequency (m = 128)                   0.380615
Cusum-Forward                               0.628308
Cusum-Reverse                               0.663369
Runs                                        0.419268
Long Runs of Ones                           0.024390
Rank                                        0.083553
Spectral DFT                                0.010186
Non-overlapping Templates (m = 9, B = 000000001)          0.165757
Overlapping Templates (m = 9)               0.296897
Universal                                   0.669012
Approximate Entropy (m = 10)                0.361595
Random Excursions (x = +1)                  0.844143
Random Excursions Variant (x = -1)          0.760966
Linear Complexity (M = 500)                 0.255475
Serial (m = 16, 2m∇Ψ )                      0.143005

Is pi a good random sequence generator? Look at the above results (or search the meanings of the left column variable, if you have no idea what they mean), and check if it satisfies your need.

sm535
  • 139
  • 1
  • 1
    The read me for Diehard says that it needs about 10-12 megabytes of binary data (the best I could find is 32 kilobytes). If you ran it against the ascii data, the test would be quite a ways off from what the application is expecting. –  Mar 21 '14 at 02:53
  • My answer was for the OP question and the original question on Math.SE - neither of which mentioned anything about ascii versus binary data or the length of the sample. Without a large enough sample set, how can the statistical randomness of any sequence be determined? – sm535 Mar 21 '14 at 12:20