So, I know that python's randint function is a prng (pseudo random number generator). I have found this article from random.org http://ift.tt/1z9RP84 that states that most modern prng's have a cycle, meaning that they look random, but they are in a pattern that will eventually repeat itself. I wrote a script to attempt to crack the cycle by generating lots of random integers ((randint(1,6))
) but for some reason, my script gets really slow when it gets past 100 000. I know that python can work with numbers better, and i'm running this on my server (128 gig ram) so the hardware shouldnt be a problem. Im wondering if maybe my scripts algorithm can be improved. here it is:
from random import *
w = []
found = False
def funct(x):
lista = [10,100,1000,10000,20000,30000,40000,50000,60000,70000,80000,90000,100000,200000,300000,400000,500000,600000,700000,800000,900000,1000000,10000000,100000000]
if x not in lista:
return False
else:
return x
while not found:
w.append(randint(1,6))
q = len(w)
if funct(q): #this was only to check the progress of the while loop
print(q)
if (q%2) == 0:
q = (int(q/2))
if w[q:] == w[:q]:
print(w)
found = True
my code is basically trying to generate this sequence twice inside a list. It checks the list to see if it is evenly divided then checks both halves of the list to see if they are equal. my questions are: Why is this slow? How can this be improved? Does python's prng even have a cycle cycle? thx in advance
Aucun commentaire:
Enregistrer un commentaire