如何使非列表循环从1-10中选择4个随机数并显示它们,但它们不能相同?

时间:2018-11-10 02:54:14

标签: python loops random numbers

import random
counter = 0
count = 0
counting = 0
valueOne = 0
valueTwo = 0
while counter ==0:
    playerOne = random.randint(1,10)
    number = random.randint(1,10)

关于这里,我感到困惑。它将运行循环,但是每次运行时,随机值都会重置,即使第一次打印随机数,第二次也可能打印相同,依此类推。

    if number == playerOne:
        count = count + 1
    if number != playerOne:
        valueOne = playerOne

        if number != valueOne:

            print("lotto number",number)
            counting = counting + 1
        if counting >= 4:
            print("it took you this many trys",count)
            input('play again?')
            counting = 0
            count = 0

1 个答案:

答案 0 :(得分:0)

应在1-10(含)之间列出数字,然后随机抽样。

random.sample(range(1,11), 4)

random.sample docs

range docs