Tuesday, 15 July 2014

python - Truly random number generator: python3.2 how to repeat -



python - Truly random number generator: python3.2 how to repeat -

i creating "truly random" number generator. takes pythons random.randint function , uses generate random numbers. turns out not random sec code trying numbers print when number of 0s equal number of 1s , number of 1s equal number of 2s..etc.. problem doesn't loops , having problem getting to.

first code; python randomness:

import random minimum= 0 maximum= 9 howmany = int(input("how many numbers want generate?")) numzero =0 numone = 0 numtwo = 0 numthree = 0 numfour = 0 numfive = 0 numsix = 0 numseven = 0 numeight = 0 numnine = 0 n in range(howmany): num = random.randint(minimum,maximum) if num == 0: numzero = numzero + 1 elif num == 1: numone = numone + 1 elif num == 2: numtwo = numtwo + 1 elif num == 3: numthree = numthree + 1 elif num == 4: numfour = numfour + 1 elif num == 5: numfive = numfive + 1 elif num == 6: numsix = numsix + 1 elif num == 7: numseven = numseven + 1 elif num == 8: numeight = numeight + 1 elif num == 9: numnine = numnine + 1 print () print () print ('0=', numzero) print ('1=',numone) print ('2=',numtwo) print ('3=',numthree) print ('4=',numfour) print ('5=',numfive) print ('6=',numsix) print ('7=',numseven) print ('8=',numeight) print ('9=',numnine) print () print ('total = ', numzero + numone + numtwo + numthree + numfour + numfive + numsix + numseven + numeight + numnine) print (num)

second approach, true randomness, doesn't work supposed to:

import random minimum=0 maximum=9 numzero =0 numone = 0 numtwo = 0 numthree = 0 numfour = 0 numfive = 0 numsix = 0 numseven = 0 numeight = 0 numnine = 0 howmany = int(input("how many numbers want generate?")) def rand(): n in range(howmany): num = random.randint(minimum,maximum) if num == 0: numzero = numzero + 1 elif num == 1: numone = numone + 1 elif num == 2: numtwo = numtwo + 1 elif num == 3: numthree = numthree + 1 elif num == 4: numfour = numfour + 1 elif num == 5: numfive = numfive + 1 elif num == 6: numsix = numsix + 1 elif num == 7: numseven = numseven + 1 elif num == 8: numeight = numeight + 1 elif num == 9: numnine = numnine + 1 numberss = (numzero, numone, numtwo, numthree, numfour, numfive, numsix, numseven, numeight, numnine) rand() numbers in numberss: if numzero == numone , numone == numtwo , numtwo == numthree , numthree == numfour , numfour == numfive , numfive == numsix , numsix == numseven , numseven == numeight , numeight == numnine: print () print () print (numzero) print (numone) print (numtwo) print (numthree) print (numfour) print (numfive) print (numsix) print (numseven) print (numeight) print (numnine) print () print (numzero + numone + numtwo + numthree + numfour + numfive + numsix + numseven + numeight + numnine) else: numzero =0 numone = 0 numtwo = 0 numthree = 0 numfour = 0 numfive = 0 numsix = 0 numseven = 0 numeight = 0 numnine = 0 print ('not random') rand() print (num)

it not work because generate 1 number. if want create loop, seek for looper in range(0, 10) should loop 10 times. changing value every time.

python loops random numbers

No comments:

Post a Comment