我创造了一个真正的随机数发生器。这需要Python随机.randint并使用它生成随机数。这并不是真正的随机性,所以第二个代码基本上是试图得到数字,只有当0的数量等于1的数量,1的数量等于2的数量,等等。。
我的问题是它没有循环,我很难让它到达。在
第一个代码;Python随机性:import random
minimum= 0
maximum= 9
howMany = int(input("How many numbers do you want to generate?"))
numZero =0
numOne = 0
numTwo = 0
numThree = 0
numFour = 0
numFive = 0
numSix = 0
numSeven = 0
numEight = 0
numNine = 0
for 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)
第二种方法是真正的随机性,但其工作原理与预期不符:
^{pr2}$