【问题标题】:How to make a function that would give one output 20% of the time, another output 10% of the time, and another output 70% of the time? [duplicate]如何制作一个函数,一个输出 20% 的时间,另一个输出 10% 的时间,另一个输出 70% 的时间? [复制]
【发布时间】:2019-11-28 22:19:40
【问题描述】:

我尝试过

import random
from statistics import mode

numlist = []
for i in range(6):
    numlist.append(random.randint(1, 4))
for i in range(3):
    numlist.append(random.randint(1, 3))
for i in range(1):
    numlist.append(random.randint(1, 2))

print(mode(numlist))

我尝试在 70% 的情况下给出 1 个数字,在 20% 的时间给出另一个数字,在 10% 的时间给出另一个数字。 但是,算一算,分别是 50%、30%、20%

【问题讨论】:

    标签: python


    【解决方案1】:
    i = random.random(0, 9)
    if i <= 1:
        <10% of the time>
    elif i <= 3:
        <20% of the time>
    else:
        <70% of the time>
    

    【讨论】:

    • 差不多。你的评论刚刚结束。 (我的意思是 <...> 中的部分)
    • 我认为您需要&lt; 而不是&lt;= 才能获得正确的百分比。
    • 对于 randint(0, 9) 你需要
    • 实际上,random.random(0, 9) 引发了 TypeError: random() takes no arguments (2 given)。也许你的意思是random.uniform(0, 9)
    • 很好,没错
    【解决方案2】:

    您正在用概率字段 p 重新发明选择

    x = np.random.choice([2,3,4], 1, p=[0.2, 0.1, 0.7])
    random.randint(1, x)
    

    【讨论】:

    • 不需要 numpy 参与其中。 lambda: random.choices([2, 3, 4], [0.2, 0.1, 0.7])
    • 与 numpy 的文档相反,random.choices 接受权重数组,其中概率是(近似)归一化为单位总和。所以random.choices([2, 3, 4], [2, 1, 7]) 是等价的。
    【解决方案3】:

    当您使用 range(6) 时,它会返回 0,1,2,3,4,5。您的代码返回一个 60-30-10 列表。

    import random
    from statistics import mode
    
    numlist = []
    for i in range(7):
        numlist.append(random.randint(1, 4))
    for i in range(2):
        numlist.append(random.randint(1, 3))
    for i in range(1):
        numlist.append(random.randint(1, 2))
    

    这里它包含在一个函数中,可让您确定输出中包含多少个十年。

    import random
    
    # stt for seven-twenty-ten
    def stt_split(n=1):
        # returns lists with lengths that are multiples of ten
        numlist = []
        for decades in range(n):
            for i in range(7):
                numlist.append(random.randint(1, 4))
            for i in range(2):
                numlist.append(random.randint(1, 3))
            for i in range(1):
                numlist.append(random.randint(1, 2))
        return numlist
    
    alice = stt_split()
    print(alice) # 10 values in a list
    
    bob = stt_split(4)
    print(bob) # 40 values in a list
    

    另一方面,如果您不希望返回具有集合分布的列表,但希望函数返回由随机选择的函数计算的值,则可以尝试类似的方法。

    import random
    
    # stt for seven-twenty-ten
    def stt_dice(n=1):
        # returns lists of random ints with length n
        numlist = []
        for decades in range(n):
            x = random.choices([2, 3, 4], [2, 1, 7])[0]    
            numlist.append(random.randint(1, x))
        return numlist
    
    alice = stt_dice()
    print(alice) # 1 value in a list
    
    bob = stt_dice(10)
    print(bob) # 10 values in a list
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多