生成具有不同数字的随机数 [英] Generating random number with different digits

查看:92
本文介绍了生成具有不同数字的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I need to write a program which generates random numbers from 100 to 999, and the tricky part is that the digits in the number can't be the same.

例如:不允许使用 222 212 等.

For example: 222, 212 and so on are not allowed.

到目前为止,我有这个:

So far, I have this:

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

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

As you can see, I generate all three digits separately. I think it's easier to check if there are same digits in the number.

因此,现在我想做一个循环,该循环将生成数字,直到满足要求为止(现在它会打印空白页或数字).请注意,它仅生成一次,因此我必须再次打开该程序.

So, now I want to do a loop that will generate the numbers until the requirements are met (now it either prints blank page or the number). Note that it generates only once and I have to open the program again.

在C ++中,我使用了' while循环 '.在这里,我不知道该怎么做.

In C++ I did that with the 'while loop'. And here I don't know how to do it.

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

And one more question. How can I code the number(quantity) of random numbers I want to generate? To be more specific: I want to generate 4 random numbers. How should I code it?

PS 谢谢大家的回答和建议,我非常热衷于学习新技术和代码.

推荐答案

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

To loop until it's ok you can also use 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)

您还可以将其放在函数中:

You can also put it in a function:

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 次:

And if you want n such numbers (they are not actually numbers since (a, b, c) is a tuple of 3 int values), you can call it n times:

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

如果您喜欢设置值的格式,也可以执行以下操作:对于范围(n)中的i的

If you prefer formatting the values, you can also do:

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

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

Finally, you can use get n from the command line:

import sys
n = sys.argv[1]

或者您可以直接问:

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

如果无法转换该值,则会出现异常;您可以捕获异常并循环进行数字生成.

You'll get an exception if the value cannot be converted; you can catch the exception and loop as for the generation of numbers.

将其与典型的 main 构造一起放入

Putting it all together with the typical main construct:

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()

这篇关于生成具有不同数字的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆