[英]How do i make a list of random unique numbers that avoid using numbers from an already existing list of random unique numbers?
I am trying to make a lottery machine that generate a list of 7 winning numbers and 3 bonus numbers from 1-34. 我正在尝试制作一种彩票机,该彩票机从1-34生成7个中奖号码和3个奖金号码的列表。 I want the bonus numbers to never pick the same numbers as the winning numbers. 我希望奖金号码永远不要选择与中奖号码相同的号码。
winning_numbers = random.sample(range(1, 34), 7)
bonus_numbers = random.sample(range(1, 34), 3)
Is there any command/code that can exclude the numbers already picked in the previous list? 是否有任何命令/代码可以排除上一个列表中已经选择的数字?
random
solution random
解
If you want to only use the random
module, you can use: 如果只想使用random
模块,则可以使用:
import random
nums = random.sample(range(1,35), 10)
winning_numbers = nums[:7]
bonus_numbers = nums[7:]
>>> winning_numbers
[2, 23, 29, 34, 26, 16, 13]
>>> bonus_numbers
[8, 4, 19]
As random.sample
is "Used for random sampling without replacement." 由于random.sample
是“用于无替换的随机抽样”。 (Quoted from the docs ) (摘自文档 )
numpy
solution numpy
解决方案
You can also use numpy
, as numpy.random.choice
has a replace
argument you can set to false
. 您还可以使用numpy
,因为numpy.random.choice
有一个replace
参数,您可以将其设置为false
。 (I'm personally a fan or using numpy
for random numbers, as it provides a lot more flexibility in more complex tasks than random
) (我个人是粉丝,或者使用numpy
表示随机数,因为它在执行更复杂的任务时比使用random
提供更大的灵活性)
import numpy as np
nums = np.random.choice(range(1,35), 10, replace=False)
winning_numbers = nums[:7]
bonus_numbers = nums[7:]
>>> winning_numbers
array([27, 4, 17, 30, 32, 21, 23])
>>> bonus_numbers
array([15, 13, 18])
A small but not unimportant remark at the beginning: 在开始时有一个简短但不重要的说明:
Whether you draw first 7 numbers and then 3 from the remaining ones, or 10 right away and split them into 7 winning numbers and 3 bonus numbers is absolutely equivalent in terms of probability : 无论您是先抽出7个数字,然后从其余数字中抽出3个,还是立即抽出10个并将其分为7个中奖数字和3个奖励数字,就概率而言,绝对相等 :
In the first case the probability for any number to become a bonus number is: 在第一种情况下,任何数字成为奖励数字的可能性为:
And in the second: 在第二个中:
Note that the reasoning for the probability of any number being a winning number is just the same. 注意,任何数字作为中奖号码的可能性的理由都是相同的。
Implementing the first case is actually slightly longer, but still fairly simple when using set
and set operations . 实际上, 实现第一种情况的时间略长,但是在使用set
和set操作时仍然相当简单。 Here is how this could look: 这是下面的样子:
import random
# set of all potential numbers to draw from
all_numbers = set(range(1,35))
# draw the winners
winning_numbers = set(random.sample(all_numbers, 7)
# subtract the winners
remaining_numbers = all_numbers-winning_numbers
# draw the bonus from the remaining numbers:
bonus_numbers = set(random.sample(remaining_numbers, 3))
The implementation of the 2nd case is minimal and could look like this: 第二种情况的实现很少,可能看起来像这样:
import random
drawn_numbers = random.sample(range(1,35), 10)
winning_numbers, bonus_numbers = drawn_numbers[:7], drawn_numbers[7:]
Hope that helped and happy coding! 希望对您有所帮助,并祝您编程愉快!
Something like this? 像这样吗
import random
winning_numbers = random.sample(range(1, 34), 7)
bonus_numbers = []
while 1==1:
num = random.randint(1,34)
if len(bonus_numbers) == 3:
break
if num not in winning_numbers and num not in bonus_numbers:
bonus_numbers.append(num)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.