【问题标题】:Generate a random number from randint() multiple times in a function in Python [duplicate]在Python中的函数中多次从randint()生成随机数[重复]
【发布时间】:2020-03-07 17:34:26
【问题描述】:

我对 python 很陌生,正在尝试创建一个基本的易经卦创建程序,但我在第一个函数中遇到了问题。

import random    

def hexline(): 
#This flips three coins and produces a hexagram line 
    flip = random.randint(2,3)
    res = flip+flip+flip
    if res == 6:
        print('-- --x')
    elif res == 7:
        print('-----')
    elif res == 8:
        print('-- --')
    elif res == 9:
        print('-----x')

hexline() 运行时,它只返回-----x-- --x(值9 或6),表明randint(2,3) 只对整个函数进行一次随机选择(而不是3 个不同的随机选择) 并将它们与res 一起添加。所以res 只生产2+2+23+3+3,而不是说3+2+32+2+3。如何在此函数中多次生成随机选择,而不仅仅是一次?谢谢。

【问题讨论】:

    标签: python


    【解决方案1】:

    当您说flip = random.randint(2,3) 时,它只调用一次randint 并将输出值设置为flip。因此,将三个翻转相加只是将值乘以三。

    一个非常简单的解决方法是创建三个变量flip1flip2flip3,每个变量都调用randint,然后将这些变量加在一起形成res。或者,一次将它们全部加在一起。

    随着您使用 Python 的进步,您将了解到您可以使用 for 循环或列表推导式产生类似的效果。您可以使用它们将任意数量的东西加在一起。这是 for 循环版本:

    total = 0
    for i in range(1, 4):
        total = total + random.randint(2,3)
    

    【讨论】:

      【解决方案2】:

      这应该可行:

      import random    
      
      def hexline(): 
      #This flips three coins and produces a hexagram line 
          res=0
          for x in range(0,3):
            res += random.randint(2,3)
          if res == 6:
              print('-- --x')
          elif res == 7:
              print('-----')
          elif res == 8:
              print('-- --')
          elif res == 9:
              print('-----x')
      

      【讨论】:

        【解决方案3】:

        在这里,您为flip 赋值一次,而不是第二次评估随机性。这意味着您正在有效地评估更多内容:

            a = 2
            res = a + a + a
        

        当然,当翻转生成3 时,上述代码的计算结果应为6,或9

        要解决您遇到的问题,您需要评估random.randint(2,3) 三次,而不是您现在正在做的评估,如下所示:

        import random    
        
        def hexline(): 
        #This flips three coins and produces a hexagram line 
        
            res = random.randint(2,3) + random.randint(2,3) + random.randint(2,3)
            if res == 6:
                print('-- --x')
            elif res == 7:
                print('-----')
            elif res == 8:
                print('-- --')
            elif res == 9:
                print('-----x')
        

        您可以定义一个小函数或 lambda 以使其更整洁。

        import random    
        
        def hexline(): 
        #This flips three coins and produces a hexagram line 
            flip = lambda: random.randint(2,3)
            res = flip()+flip()+flip()
            if res == 6:
                print('-- --x')
            elif res == 7:
                print('-----')
            elif res == 8:
                print('-- --')
            elif res == 9:
                print('-----x')
        

        这里发生的情况与第一个解决方案几乎相同,但它通过将 random.randint(2,3) 定义为 lambda 将其重命名为 flip()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-05
          • 1970-01-01
          • 2020-03-02
          • 2020-07-04
          • 1970-01-01
          • 2023-01-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多