生成具有不同数字的随机数

编程入门 行业动态 更新时间:2024-10-27 00:25:43
本文介绍了生成具有不同数字的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我需要编写一个程序来生成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()

更多推荐

生成具有不同数字的随机数

本文发布于:2023-10-28 23:08:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!