python-3.x - 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)
再次运行这行代码。
这里发生了什么?
解决方案
我刚刚添加 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))
推荐阅读
- android - 这种基于多个 TImage 的可滚动 UI 设计对 Android 来说是错误的吗?
- jquery - jquery datatable ajax 没有可用的数据 mvc
- python - 熊猫合并或加入较小的数据框
- batch-file - bat 文件回显多行
- mysql - 在MySql中一对多关系的情况下,如何根据同一行另一列的值获取一列的值
- amazon-web-services - 如何使用MWS API区分亚马逊上同一卖家的两个不同商店的订单?
- angular - 如何使用Angular6在父组件中添加多个组件
- javascript - JavaScript 数组与对象的性能
- regex - 正则表达式的无效匹配('\pL' 在字符类中不起作用)
- google-places-api - 谷歌地方,不同的 api 密钥得到“你已经超出了这个 API 的每日请求配额”。