Skip to main content
 首页 » 编程设计

python之获取范围内随机数选择中整数出现次数 N 次

2025年05月04日13JeffreyZhao

<分区>

在编写这段代码时,我发现自己做了很多重复性的事情,并且想知道是否有更简单、更简单或更短的方法来完成这样的重复性任务。

相关代码如下:

 from random import randint 
    RandomNumber = Zeroes = Ones = Twos = Threes = Fours = Fives = Sixes = i = 0 
 
    while i < 1000000: 
        RandomNumber = (randint(0,6)) 
        if RandomNumber == 0: 
            Zeroes = Zeroes + 1 
        if RandomNumber == 1: 
            Ones = Ones + 1 
        if RandomNumber == 2: 
            Twos = Twos + 1 
        if RandomNumber == 3: 
            Threes = Threes + 1 
        if RandomNumber == 4: 
            Fours = Fours + 1 
        if RandomNumber == 5: 
            Fives = Fives + 1 
        if RandomNumber == 6: 
            Sixes = Sixes + 1 
 
        i = i + 1