如何在python中的特定范围内生成n维随机变量 [英] How to generate n dimensional random variables in a specific range in python
本文介绍了如何在python中的特定范围内生成n维随机变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在python中各种尺寸的[-10,10]
范围内生成统一的随机变量. 2,3,4,5 ....维数.
I want to generate uniform random variables in the range of [-10,10]
of various dimensions in python. Numbers of 2,3,4,5.... dimension.
我尝试过random.uniform(-10,10),但这只是一维.我不知道该怎么做. 我的意思是2维,
I tried random.uniform(-10,10), but that is only one dimensional. I do not know how to do it for n-dimension. By 2 dimension I mean,
[[1 2], [3 4]...]
推荐答案
由于已标记numpy
,因此可以在numpy.random
中使用随机函数:
Since numpy
is tagged, you can use the random functions in numpy.random
:
>>> import numpy as np
>>> np.random.uniform(-10,10)
7.435802529756465
>>> np.random.uniform(-10,10,size=(2,3))
array([[-0.40137954, -1.01510912, -0.41982265],
[-8.12662965, 6.25365713, -8.093228 ]])
>>> np.random.uniform(-10,10,size=(1,5,1))
array([[[-3.31802611],
[ 4.60814984],
[ 1.82297046],
[-0.47581074],
[-8.1432223 ]]])
并修改size
参数以适合您的需求.
and modify the size
parameter to suit your needs.
这篇关于如何在python中的特定范围内生成n维随机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文