【问题标题】:Simple Lottery: Generate new six unique random numbers between 1 and 49简单彩票:生成 1 到 49 之间的新六个唯一随机数
【发布时间】:2018-03-19 05:23:49
【问题描述】:
  • 如果有五个“短号”或所有六个都是“短号”(短号为 1
  • 如果有五个“大数”或所有六个都是“大数”(大数定义为 25
  • 如果六个数字中至少有五个是偶数
  • 如果六个数字中至少有五个是奇数
  • 如果至少三个数字是连续的数字(例如 [13, 14, 15, 28, 35, 49] --> 绘制新的六。或者另一个示例是例如 [5, 6, 7, 8, 21 , 38] --> 绘制新的六个数字)

我开始编写列表中的前两个:

import random

def lottery_six():
    setOfSix = set()
    while len(setOfSix) < 6:
        setOfSix.add(random.randint(1,49))
    lottery = list(setOfSix)
    return lottery

def generateLottery(lottery):
    abc = set()
    while (all(i >= 25 for i in lottery) == True) or (all(i < 25 for i in lottery) == True) or \
    (sum(i >= 25 for i in lottery) >= 5) or (sum(i < 25 for i in lottery) >= 5):
        abc = lottery_six()
    return abc

print(generateLottery(lottery_six()))

但是,这不起作用。为什么?我该如何解决?

【问题讨论】:

  • 如果满足这些条件,你就赢了?
  • 你用了一个集合,你是不是假设同一个值不能重复?这样的列表像 [1,1,1,1,1,1] 是不允许的?列表必须都是唯一值?
  • 是的,数字必须是唯一的。
  • 不,只是要生成一个满足上述要求的集合。
  • 上面的1个,因为条件1和2是互斥的,对吧?

标签: python random integer


【解决方案1】:

考虑到这一点,我们将重复此代码,直到找到一组合适的值。首先,我们将取范围 [1,49],我们将随机排列它们并取前 6 个值。然后我们检查范围是否满足前两个要求中的任何一个。如果是,我们会中断循环并保留这个值列表。

while True:
    x = (np.random.permutation(49)+1)[0:6]
    if len([i for i in x if 1<=i<25]) > 4: break
    if len([i for i in x if 25<=i<=49]) > 4: break

print(x)

你的整个代码可以写成

while True:
    x = (np.random.permutation(49)+1)[0:6]    
    # Check for short
    if len([i for i in x if 1<=i<25]) > 4: break
    # Check for large
    if len([i for i in x if 25<=i<=49]) > 4: break
    # Check for even
    if len([i for i in x if i%2 == 0]) > 5: break 
    # Check for odd
    if len([i for i in x if (i+1)%2 == 0]) > 5: break 
    # Check for successive
    if len([i for ix, i in enumerate(x[0:-2]) 
            if (x[ix+1] - i == 1 and x[ix+2] - x[ix+1] == 1)]) > 0: break

print(x)

这将找到满足您条件的列表。最后一条语句有点密集,分解它遍历列表中的每个值并检查您是否有至少 3 个连续值x[ix+1] - i == 1 and x[ix+2] - x[ix+1] == 1。如果这是真的,我们将值添加到列表中,如果最后在这个新列表中至少有 1 个值,我们可以得出结论,至少有 3 个连续值。

【讨论】:

    【解决方案2】:
    import random
    
    def lottery_six():
        setOfSix = set()
        while len(setOfSix) < 6:
            setOfSix.add(random.randint(1,49))
        lottery = list(setOfSix)
        return lottery
    
    def generateLottery(lottery):
        abc = set(lottery) #Modified this Line
        while (all(i >= 25 for i in lottery) == True) or (all(i < 25 for i in lottery) == True) or (sum(i >= 25 for i in lottery) >= 5) or (sum(i < 25 for i in lottery) >= 5):
            abc = lottery_six()
        return abc
    
    print(generateLottery(lottery_six()))
    

    【讨论】:

    • 除了我,你还做了什么?我无法弄清楚,但你的确实有效。
    • 我在修改的行后面添加了注释
    【解决方案3】:

    您的代码似乎没有进入循环,您可能会在 while 循环之前生成一个集合。这些行似乎也是多余的:

    (all(i &gt;= 25 for i in lottery) == True) or (all(i &lt; 25 for i in lottery) == True)

    最终代码:

    def generateLottery(lottery):
        lottery = lottery_six()
        while sum(i >= 25 for i in lottery) >= 5 or sum(i < 25 for i in lottery) >= 5:
            lottery = lottery_six()
        return lottery
    
    print(generateLottery(lottery_six()))
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2014-12-09
      • 1970-01-01
      相关资源
      最近更新 更多