Python随机数多次 [英] Python random numbers multiple times
本文介绍了Python随机数多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import random
def main():
uinput()
def uinput():
times = int(input('How many numbers do you want generated?'))
number1 = int(input('Put in the first number:' ))
number2 = int(input('Put in the second number:'))
rnumber = random.randint(number1, number2)
print (rnumber)
main()
我正在使用 Python,我希望我的程序生成随机数.如您所见,我已经完成了此操作.我要做的下一件事是让它生成多个随机数,具体取决于用户"想要生成的数字数量.我该怎么做呢?我假设需要一个循环.
I am messing around in Python, and I want my program to generate random numbers. As you can see I have already accomplished this. The next thing I want to do is have it generate multiple random numbers, depending on how many numbers the "user" wants generated. How would I go about doing this? I am assuming a loop would be required.
推荐答案
这将产生一个范围为 number1
到 number2
This will produce a list of random integers in range number1
to number2
rnumber = [random.randint(number1, number2) for x in range(times)]
有关更多信息,请查看列表理解.
这篇关于Python随机数多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文