首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中生成唯一数字,而不使用列表、集合等

在Python中生成唯一数字,而不使用列表、集合等
EN

Stack Overflow用户
提问于 2012-11-23 12:51:29
回答 4查看 377关注 0票数 3

这是一个带回家的测试题(如果Gruhn教授正在无情地搜索stackoverflow,我在20分钟前邮寄了这个问题)。第一门计算机科学课程,使用Python的入门。使用“从Python第二版开始”这本书。测试基本上是关于创建我们自己的模块库,读写文件,以及try/except逻辑。

第一部分要求创建一个彩票号码模拟器。一个生成不唯一的数字,另一个是唯一的、不重复的数字。我在这里看到的每个答案都使用了列表,不幸的是,这是下一章,我们被明确禁止使用它们。

我在本节中的代码如下:

代码语言:javascript
复制
import random

def ballPickerOne():
    a = random.randint(1, 59)
    b = random.randint(1, 59)
    c = random.randint(1, 59)
    d = random.randint(1, 59)
    e = random.randint(1, 59)
    f = random.randint(1, 35)

    showNumbers(a,b,c,d,e,f)


def ballPickerTwo():
   a = random.randrange(1,59,2)
   b = random.randrange(1,59,3)
   c = random.randrange(1,59,5)
   d = random.randrange(1,59,7)
   e = random.randrange(1,59,11)
   f = random.randint(1,35)

   showNumbers(a,b,c,d,e,f)


def showNumbers(a,b,c,d,e,f):

   print("Your Numbers ...")
   print()
   print("Ball 1: ", a)
   print("Ball 2: ", b)
   print("Ball 3: ", c)
   print("Ball 4: ", d)
   print("Ball 5: ", e)
   print("Red Ball: ", f)
   print()
   print("Good Luck")

我们需要使用showNumbers函数来显示结果,并以由此产生的格式显示结果。ballPickerTwo是“唯一的”,我在一次尝试唯一性失败时使用了质数间隔。我尝试使用循环,但不知道如何显示使用showNumbers生成的数字。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-11-23 12:59:36

这是一种非常单调乏味的方式,但它不使用列表。它将选取随机且唯一的值。

代码语言:javascript
复制
def ballPickerTwo():

    a = random.randint(1, 59)

    b = a        
    while b == a:
        b = random.randint(1, 59)

    c = b
    while c == b or c == a:
        c = random.randint(1, 59)

    d = c
    while d == c or d == b or d == a:
        d = random.randint(1, 59)

    ...
票数 1
EN

Stack Overflow用户

发布于 2012-11-23 12:55:57

只需返回您生成的值-在您的函数中使用return。例如:

代码语言:javascript
复制
def ballPickerOne():
    a = random.randint(1, 59)
    b = random.randint(1, 59)
    c = random.randint(1, 59)
    d = random.randint(1, 59)
    e = random.randint(1, 59)
    f = random.randint(1, 35)
    return a,b,c,d,e,f

showNumbers(a,b,c,d,e,f)

如果:

代码语言:javascript
复制
from random import sample, randint

def ballPickerOne():
    a,b,c,d,e = sample(range(1,59), 5) 
    f = randint(1,35)
    while f!=a and f!=b and f!=c and f!=d and f!=e:
        f = randint(1,35)
    return a,b,c,d,e,f
票数 0
EN

Stack Overflow用户

发布于 2012-11-23 13:36:08

如何使用一个整数作为位图来检查唯一性?

代码语言:javascript
复制
import random

def showNumbers(a,b,c,d,e,f):
   print("Your Numbers ...")
   print()
   print("Ball 1: ", a)
   print("Ball 2: ", b)
   print("Ball 3: ", c)
   print("Ball 4: ", d)
   print("Ball 5: ", e)
   print("Red Ball: ", f)
   print()
   print("Good Luck")

def ballPickerTwo():
    while True:
        a = random.randint(1, 59)
        b = random.randint(1, 59)
        c = random.randint(1, 59)
        d = random.randint(1, 59)
        e = random.randint(1, 59)
        f = random.randint(1, 35)
        m = 2**a + 2**b + 2**c + 2**d + 2**e + 2**f
        if bin(m).count("1") == 6:
            break
    showNumbers(a,b,c,d,e,f)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13523283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档