我想为数组的每个整数生成随机数。例如,我有一个数组[4,10,15]。我想在python中生成4个随机数,10个随机数和15个随机数
预先感谢
答案 0 :(得分:0)
import random
array = [4, 10, 15]
result = [[random.randint(YOUR_MIN_NUMBER, YOUR_MAX_NUMBER) for _ in range(i)] for i in array]
输出:
[[4,4,5,3],[7,7,7,6,3,6,3,6,8,9],[6,3,6,4,4,1,3,5 ,10、3、10、7、9、1、1、3、1]]
答案 1 :(得分:0)
以下内容从1d列表中生成2d列表,其中每个子列表具有与1d列表中相应元素l
一样多的随机数。
import random
l = [4, 10, 15]
[[random.random()]*n for n in l]
其中,给出以下漂亮的输出:
[
[
0.9471464389026101,
0.9471464389026101,
0.9471464389026101,
0.9471464389026101
],
[
0.7127486495769667,
0.7127486495769667,
0.7127486495769667,
0.7127486495769667,
0.7127486495769667,
0.7127486495769667,
0.7127486495769667,
0.7127486495769667,
0.7127486495769667,
0.7127486495769667
],
[
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407,
0.5239644545312407
]
]
答案 2 :(得分:0)
请仔细阅读此https://machinelearningmastery.com/how-to-generate-random-numbers-in-python/ 您可以解决几乎所有与随机相关的问题。