I am creating what strives to be a truly random number generator. As of right now, the implementation uses a mix of mainly nonsense system data and some pseudo-random numbers to generate numbers that are different and use the last half of the number (which, statistically, I believe is sufficiently random). The advice I would like:
Obviously, a general "review", as always.
How can I optimize this code to be faster? Currently, generating ten 1000 digit numbers takes two seconds, ten 10000 digit numbers takes 35-37 seconds, and ten 100000 digit numbers is exponentially slower.
How practical is this? Currently the generator uses some methods which may be considered "hacky"; ergo, using the last half of a number, or including pseudo-random numbers in the algorithm. Is this OK? I believe it should be, as long as the numbers generated are indeed random, but I would like other's opinions on this.
Another thing to note is that when testing some sample batches with this online tester, I got p-values around 0.24, which I believe is the optimal range. If you answer, don't feel pressured to answer all three questions.
import os, re, time, random
from functools import reduce
def rand_num(digits=3):
nums = []
strs = []
rand = []
add = True
all_info = re.sub('(.*?: *)','',os.popen("cat /proc/meminfo").read()).replace('kB','').splitlines()+list(filter(None,re.findall(r'(\w*)',os.popen('ps').read())))
all_info = list(x.strip() for x in all_info)
nums.append(random.sample(range(digits*70), digits*35))
nums = nums[0]
for x in all_info:
if x.isdigit():
nums.append(int(x))
else:
strs.append(x)
jugglenum = 0
for x in nums:
if add == True:
jugglenum += x
add = False
else:
jugglenum -= x
add = True
jugglenum = abs(jugglenum)
for i in range(digits):
rand.append(int(time.time()*(os.getpid()%jugglenum)))
for i, x in enumerate(rand):
rand[i] = ''.join(random.sample(str(x), len(str(x))))
rand = [int(x)//10 for x in rand]
largenum = str(reduce(lambda x, y: x*y, rand))[:digits]
return ''.join(random.sample(random.sample(largenum,len(largenum)),len(largenum)))
An executable program on repl.it.
Yes, I know, this is similar to random.SystemRandom
or os.urandom
. This program I am developing now is really my first look into this concept and I don't plan for this to be my final program by any means. This is just something that I thought I would play around with for the time being.
So before you go and tell me that this is pointless and could have no application, I know. This is just my own personal experiment so I can get better at this sort of program.