简体   繁体   English

如何使用randomint python获取唯一数字?

[英]How to get unique numbers using randomint python?

I am creating a 'Euromillions Lottery generator' just for fun and I keep getting the same numbers printing out. 我正在创建一个“欧洲百万富翁彩票生成器”,只是为了好玩,我不断打印出相同的数字。 How can I make it so that I get random numbers and never get the same number popping up: 我如何做到这一点,以便获得随机数,而永远不会弹出相同的数字:

from random import randint

numbers = randint(1,50)

stars = randint(1,11)


print "Your lucky numbers are: ", numbers, numbers, numbers, numbers, numbers

print "Your lucky stars are: " , stars, stars

The output is just: 输出是:

>>> Your lucky numbers are:  41 41 41 41 41
>>> Your lucky stars are:  8 8
>>> Good bye!

How can I fix this? 我怎样才能解决这个问题?

Regards 问候

You are generating one number then printing that out several times. 您正在生成一个数字,然后多次打印出来。

Generate several numbers instead: 而是生成几个数字:

print "Your lucky numbers are: ", randint(1,50), randint(1,50), randint(1,50), randint(1,50), randint(1,50)

or generate a list: 或生成列表:

numbers = [randint(1,50) for _ in range(5)]
print "Your lucky numbers are: ", ' '.join(numbers)

or better still, generate all permissible numbers (using range() then pick a sample from that: 或更妙的是,生成所有允许的数字(使用range()然后从中选择一个样本:

possible_numbers = range(1, 51)  # end point is not included
numbers = random.sample(possible_numbers, 5)
print "Your lucky numbers are: ", ' '.join(map(str, numbers))

Now numbers is guaranteed to consist of entirely unique numbers. 现在,保证numbers由完全唯一的数字组成。

The numbers variable does not magically update every time you print it because it refers only to the result of random.randint(1, 50) . numbers变量,因为它仅指的结果不会奇迹般地更新每次打印时random.randint(1, 50)

Set up a set of numbers then shuffle and slice the set. 设置一组数字,然后将其洗牌并切成薄片。

from random import shuffle
numbers  = list(range(1,51))
shuffle(numbers)
draw = numbers[:6]
print(draw)

numbers = randint(1,50) assigns one random number to a variable. numbers = randint(1,50)为变量分配一个随机数。 And you repeatedly use this one random number. 然后您反复使用这个随机数。 Same goes for stars stars

Try this instead: 尝试以下方法:

print "Your lucky numbers are: ", randint(1,50), randint(1,50), randint(1,50), randint(1,50), randint(1,50) 

Or you can create a list of numbers and get a random sample: 或者,您可以创建数字列表并获取随机样本:

numbers = range(1,50)
print "Your lucky numbers are: ", ' '.join(map(str, random.sample(numbers, 5)))

You should call randint 5 times for your lucky numbers. 您应该致电randint 5次以获取您的幸运数字。 You only call it once, and display the result 5 times. 您只需调用一次,并显示5次结果。

You can use 您可以使用
random.sample(range(range_to_which_random_be_generated), number_of_numbers_to_generate)

example
random.sample(range(50),6)
would generate 6 different numbers in range 0 to 49 会产生0至49范围内的6个不同的数字

One way i would suggest is to put the generated numbers in a list, and check if the next number is not in the list list before adding it to the list 我建议的一种方法是将生成的数字放在列表中,并在将下一个数字添加到列表中之前检查下一个数字是否不在列表中

numbers=[randint(1,50)]
i=1
while i<n:  # n is the number of random numbers you want to generate
    x=randint(1,50)
    if not x in numbers:
        i+=1
        numbers.append(x)

The problem in your code is that you generate only a single random number and print it four times 您的代码中的问题是您只生成一个随机数并打印四次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM