【问题标题】:Generating random number with different digits生成具有不同数字的随机数
【发布时间】:2015-05-24 21:08:52
【问题描述】:

所以我需要编写一个程序来生成从100到999的随机数,而棘手的部分是数字中的数字不可能相同。

例如:222、212等是不允许的。

到目前为止,我有这个:

import random

a = int (random.randint(1,9)) <-- first digit
b = int (random.randint(0,9)) <-- second digit
c = int (random.randint(0,9)) <-- third digit

if (a != b and a != b and b != c):
    print a,b,c

如您所见,我分别生成所有三个数字。我认为检查号码中是否有相同的数字更容易。

所以,现在我想做一个循环来生成数字,直到满足要求(现在它要么打印空白页,要么打印数字)。请注意,它只生成一次,我必须再次打开程序。

在 C++ 中,我使用“while 循环”来做到这一点。在这里我不知道该怎么做。

还有一个问题。如何编码要生成的随机数的数量(数量)? 更加具体: 我想生成 4 个随机数。我应该如何编码?

附言 感谢大家的回答和建议,我非常热衷于学习新技术和代码。

【问题讨论】:

  • 所以你并没有真正生成随机数。

标签: function python-2.7 loops random while-loop


【解决方案1】:

使用random.sample 从分布[0,9] 中进行抽样可能更容易,无需替换,拒绝任何首先选择0 的样本:

import random

def pick_number():
    a = 0
    while a == 0:
        a, b, c = random.sample(range(10), 3)
    return 100*a + 10*b + c

进一步解释:range(10)(在 Python 2 中)生成列表 [0,1,2,3,4,5,6,7,8,9],random.sample 从中选取 3 个元素而不进行替换。 while 循环重复直到第一个元素,a 不为零,因此当它退出时,您将获得满足要求的数字的三位数字。您可以通过将第一个乘以 100,第二个乘以 10,然后将所有三个相加来将其转换为单个整数。

【讨论】:

  • 进一步解释您的代码究竟做了什么可能会有所帮助。
【解决方案2】:

要循环直到没问题,您也可以在 Python 中使用 while:

from random import randint

a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
while not (a!=b and b!=c and c!=a):
    a = randint(1, 9)
    b = randint(0, 9)
    c = randint(0, 9)

你也可以把它放在一个函数中:

def generate_number():
    a = randint(1, 9)
    b = randint(0, 9)
    c = randint(0, 9)
    while not (a!=b and b!=c and c!=a):
        a = randint(1, 9)
        b = randint(0, 9)
        c = randint(0, 9)
    return (a, b, c)

如果你想要 n 这样的数字(它们实际上不是数字,因为 (a, b, c) 是 3 个 int 值的元组),你可以调用它 n 次:

for i in range(n):
    print(generate_number())

如果您更喜欢 formatting 的值,您也可以这样做:

for i in range(n):
    print('%d %d %d'%generate_number()) # old style formatting
    print('{} {} {}'.format(*generate_number())) # new style

最后,您可以从命令行使用 get n:

import sys
n = sys.argv[1]

也可以直接问:

n = int(input("Please enter some number: ")) # in python2.x you'd use raw_input instead of input

如果值不能转换,你会得到一个异常;您可以捕获异常并循环生成数字。

将它们与典型的main 构造放在一起:

from random import randint
import sys

def generate_number():
    a = randint(1, 9)
    b = randint(0, 9)
    c = randint(0, 9)
    while not (a!=b and b!=c and c!=a):
        a = randint(1, 9)
        b = randint(0, 9)
        c = randint(0, 9)
    return (a, b, c)

def main():
    n = sys.argv[1]
    for i in range(n):
        print('{} {} {}'.format(*generate_number()))

if __name__=='__main__':
    main()

【讨论】:

  • 谢谢你们两个^^你们真的帮了我。我不知道,也许我很乐观,但我真的很接近(到目前为止)Francis Colas 写的代码......再次感谢你:) 我只是不喜欢它打印的一件事像这样的数字 [1,0,9] 而不是 1 0 9 (是的,我知道它们是单独的数字),但也许有可能获得更好的结果? x_X 此外,我不知道为什么,但是..这个'for循环'在这里工作很奇怪(与c ++相比),我想我可能做错了什么,因为我试图这样做这个'n'会是来自键盘的输入...
【解决方案3】:

不要生成 3 个随机数,而是创建一个 0-9 的数字列表,然后在其上调用 random.shuffle。然后,您只需从打乱列表中提取所需数量的数字,就不会重复任何数字

import random

def generate_number():
    numbers = range(10) # Generates a list 0-9
    random.shuffle(numbers) # Shuffle the list

    return (numbers[0], numbers[1], numbers[2]) #take the first 3 items

(这个答案与xnx's answer execpt 几乎相同,它使用shuffle 方法,可以在2.3 之前的版本中使用,它不会循环等待非0 的第一个数字。)

【讨论】:

    【解决方案4】:

    在@xnx 答案之上,一些背景知识:这称为Fisher-Yates(或F-Y-Knuth)洗牌,它类似于随机挑选彩票,只使用一次。 O(n) 复杂度,在此处阅读更多内容http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多