I think the best Random Number on the Planet is PI. I wrote a code that generates a list of pseudo-random numbers in Python. Then it treats this list as positions of digits in Pi and then outputs digits at those positions. I am expecting someone to tell me that this method is NOT RANDOM ENOUGH.
import random
# Read all decimal places of PI from the file
# and store as a string of 1 Million digits
with open("piDigits.txt", 'r') as f:
pidecimals = f.read()
f.close()
# Generate a list of random numbers
# Treat each random number as a position in the expansion of Pi
# Go to that position and pick up a particular number of digits of Pi
# Output the results as a list
def generate_random_array(nums, lengthofnum):
randomnumbers=[]
random_array = random.sample(range(1,len(pidecimals)-lengthofnum), nums)
for i in range(0, nums):
randomnumbers.append(pidecimals[random_array[i]:random_array[i]+lengthofnum])
print(randomnumbers)
# Main Program
mychoice = int(input("How many random numbers do you want? "))
mylength = int(input("How many digits in each random number? "))
generate_random_array(mychoice, mylength)
Sample Output:
How many random numbers do you want? 10000
How many digits in each random number? 2
['55', '37', '93', '63', '36', '35', '72', '81', '01', '46', '90', '98', '31', '38', '65', '57', '10', '42', '07', '72', '11', '68', '48', '33', '08', '43', '28', '64', '75', '48', '00', '37', '74', '05', '97', '72', ,...]
After the discussion in the comments below, I did a further experiment. I generated a series of quantum random numbers using the quantumrandom library. Then I extracted the decimal part of it and then extracted some digits from it. Then I tested for the same string being present in PI. VOILA! They did!!!
import quantumrandom as qr
with open("piDigits.txt", 'r') as f:
pidecimals = f.read()
f.close()
maxrange = int(input("Enter Maxmimum of Range "))
count = 0
for i in range (0,maxrange):
myx = qr.randint()
decimalstring = str(myx)
substring = decimalstring[2:7]
if substring in pidecimals:
count += 1
print(f"Quantum digit sequence {substring} found in Pi")
print(f"Quantum digit sequences found in Pi {count} times out of {maxrange}")
For 5 digits and 10 quantum random numbers...
Enter Maxmimum of Range 10
Quantum digit sequence 39467 found in Pi
Quantum digit sequence 58304 found in Pi
Quantum digit sequence 11932 found in Pi
Quantum digit sequence 93949 found in Pi
Quantum digit sequence 68543 found in Pi
Quantum digit sequence 84222 found in Pi
Quantum digit sequence 10429 found in Pi
Quantum digit sequence 12771 found in Pi
Quantum digit sequence 70878 found in Pi
Quantum digit sequence 08628 found in Pi
Quantum digit sequences found in Pi 10 times out of 10
For 6 digits and 10 quantum random numbers:
Enter Maxmimum of Range 10
Quantum digit sequence 122453 found in Pi
Quantum digit sequence 817349 found in Pi
Quantum digit sequence 719539 found in Pi
Quantum digit sequence 609292 found in Pi
Quantum digit sequence 824750 found in Pi
Quantum digit sequence 833676 found in Pi
Quantum digit sequences found in Pi 6 times out of 10
The program also finds sequences if I truncate the random from the end - so it matches the second half of the quantum number to a sequence in PI.
Maybe that is all that PI is - juxtaposition of quantum random numbers. :)
2 Answers 2
The fundamental premise of this solution is incorrect. The existing PRNG (random.sample
) is defined thus:
Almost all module functions depend on the basic function
random()
, which generates a random float uniformly in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence.
Using that as the index for lookup in a substitution table of fixed digits whose distribution is uniform - no matter what they are, pi or otherwise - does not increase entropy. So it's incorrect to say that it's more (or less) random than random.sample()
; it's exactly as random as random.sample()
. It's just slower, occupies more memory and disk space. Don't do this.
If you want something "more random": why? If it's frivolous, just use the built-in random module. If it's for security purposes, use secrets.
-
\$\begingroup\$ When you say the "Distribution of decimal expansion of Pi is uniform...", what does that mean...!? \$\endgroup\$Pinchi– Pinchi2022年03月19日 18:56:50 +00:00Commented Mar 19, 2022 at 18:56
-
\$\begingroup\$ Read e.g. math.stackexchange.com/questions/51829/… \$\endgroup\$Reinderien– Reinderien2022年03月19日 19:08:37 +00:00Commented Mar 19, 2022 at 19:08
-
\$\begingroup\$ Yeah, hasn't been proved. I take the point but wonder if there is an objective test of how something like the quantumrandom or secret library provides random number with more entropy than a randomly chosen chunk of digits of the expansion of Pi 🤷♂️ \$\endgroup\$Pinchi– Pinchi2022年03月19日 19:28:00 +00:00Commented Mar 19, 2022 at 19:28
-
\$\begingroup\$ You still haven't answered "why". \$\endgroup\$Reinderien– Reinderien2022年03月19日 19:33:52 +00:00Commented Mar 19, 2022 at 19:33
-
\$\begingroup\$ The answer is "Why Not" but thanks for answering :) \$\endgroup\$Pinchi– Pinchi2022年03月19日 19:37:48 +00:00Commented Mar 19, 2022 at 19:37
The generation method is flawed, and it is easy to see why. Simply ask for a random number with 1 million digits. Run the program again, and ask for a new 1 million digit random number. Not very random, if it always gives you the same value. And if you ask more than 1 random number, the program crashes!
You can get only two different 999,999 digit random numbers, and only three 999,998 digit random numbers, and so on. This restrictive result is abusing the limit of only providing π to one million digits, but it is indicative of a flaw in the algorithm — it will always have a limit, restricting the entropy of the random number generation ... a limit far smaller than the Mersenne Twister algorithm built into Python.
random
. What are your objectives with this method? If you care about high-quality entropy, you have some reading to do re. operating system PRNG sources. \$\endgroup\$