在编写类似彩票的代码时,应该产生一个由6个数字组成的列表,如下所示(20,45,11,16,09,+ 12)
我该怎么做,以便它给我最后一行所有5个随机选择的值以及一个列表中所有的附加数字:
await
答案 0 :(得分:0)
我不熟悉彩票的运作方式。我假设所有数字(包括附加数字)都是从同一集合中得出的,无需替换。如果是这样,此代码应为您解决问题:
import random
numbers = list(range(1, 51)) # include 50
random.shuffle(numbers)
print(numbers[:6]) # pick the first 6 numbers (5 + 1 additional number) in the randomize list
如果您的其他电话号码来自另一套电话,请执行以下操作:
import random
numbers = list(range(1, 51)) # include 50
random.shuffle(numbers)
# pick the first 5 from the randomized list:
picks = numbers[:5]
# select from remaining numbers those that are less than or equal to 20:
picks.append(random.choice([n for n in numbers[5:] if n <= 20]))
print(picks)