首页 > 解决方案 > Python Random 没有函数不运行就刷新

问题描述

我的功能如下。我正在输入 2 个号码,在它们之间找到随机号码,然后将它们全部相乘。

start = int(input("Enter start value"))
end =   int(input("Enter end value"))

import random

numlist = random.sample(range(start,end),10)
print("list of random nos are" + str(numlist))
from functools import reduce

prod = reduce(lambda x,y: x*y, numlist)
print("the product of random nos: {0} is {1}".format(numlist,prod))

输出如下

Enter start value7
Enter end value89
list of random nos are: [58, 13, 47, 43, 44, 56, 86, 14, 25, 71]
the product of random nos: [51, 30, 7, 25, 49, 29, 35, 54, 27, 67] is 1300840136977500

我的问题是a)为什么随机编号列表会发生变化(第一行[58,13..第二行[51,30...),即使我没有numlist = random.sample(range(start,end),10)再次运行这行代码。

这里发生了什么?

标签: python-3.xrandom

解决方案


我刚刚添加 print(numlist)了随机数并没有改变我。试试这个

end =   int(input("Enter end value"))

import random

numlist = random.sample(range(start,end),10)
print(numlist)
from functools import reduce

prod = reduce(lambda x,y: x*y, numlist)
print("the product of random nos: {0} is {1}".format(numlist,prod)) 

推荐阅读