生成具有不同数字的随机数
Generating random number with different digits
所以我需要编写一个程序来生成从 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 个随机数。我应该如何编码?
P.S.
谢谢大家的回答和建议,我真的很想学习新技术和代码。
要循环直到没问题你也可以使用 while in Python:
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()
使用 random.sample
从分布 [0,9] 中采样而不放回可能更容易,拒绝任何 select 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,然后将所有三个相加,从而将其变成一个整数。
不是生成 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
(这个答案与 execpt 几乎相同它使用方法 shuffle
可以在 2.3 之前的版本中使用并且它不会循环等待非 0 第一个数字.)
在@xnx 回答之上,一些背景:这称为 Fisher-Yates(或 F-Y-Knuth)洗牌,类似于随机抽取彩票,只使用一次票。 O(n) 复杂度,更多信息请阅读此处 http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
所以我需要编写一个程序来生成从 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 个随机数。我应该如何编码?
P.S. 谢谢大家的回答和建议,我真的很想学习新技术和代码。
要循环直到没问题你也可以使用 while in Python:
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()
使用 random.sample
从分布 [0,9] 中采样而不放回可能更容易,拒绝任何 select 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,然后将所有三个相加,从而将其变成一个整数。
不是生成 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
(这个答案与 shuffle
可以在 2.3 之前的版本中使用并且它不会循环等待非 0 第一个数字.)
在@xnx 回答之上,一些背景:这称为 Fisher-Yates(或 F-Y-Knuth)洗牌,类似于随机抽取彩票,只使用一次票。 O(n) 复杂度,更多信息请阅读此处 http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle