真正的随机数生成器:python3.2如何重复

时间:2014-11-10 21:48:43

标签: python loops random numbers

我正在创建一个“真正随机”的数字生成器。这需要pythons random.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)

第二种方法,真正的随机性,但不能像它应该的那样工作:

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 do you want to generate?"))
def Rand():
    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
        numberss = (numZero, numOne, numTwo, numThree, numFour, numFive, numSix, numSeven, numEight, numNine)
Rand()
for numbers in numberss:
    if numZero == numOne and numOne == numTwo and numTwo == numThree and numThree == numFour and numFour == numFive and numFive == numSix and numSix == numSeven and numSeven == numEight and 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)

1 个答案:

答案 0 :(得分:0)

它不起作用,因为您只生成一个数字。如果你想让它循环,试试for looper in range(0, 10)这应该循环10次。但是你每次都在改变价值。