[英]Initializing a matrix with random number from given range - python
How to initialize a matrix with random number (say 0 to 0.01)? 如何初始化具有随机数(例如0到0.01)的矩阵?
A = numpy.random.rand(2,3)
This gives the result: 结果如下:
>>A
array([[ 0.45378345, 0.33203662, 0.42980284],
[ 0.2150098 , 0.39427043, 0.10036063]])
But how to specify the range of random numbers ( 0 to 0.01)? 但是如何指定随机数的范围(0到0.01)?
The numbers returned by numpy.random.rand
will be between 0 and 1. Knowing that, you can just multiply the result to the given range: numpy.random.rand
返回的numpy.random.rand
将介于0和1之间。知道这一点,您可以将结果乘以给定范围:
# 0 to 0.001
A = numpy.random.rand(2,3) * 0.01
# 0.75 to 1.5
min = 0.75
max = 1.5
A = ( numpy.random.rand(2,3) * (max - min) ) + min
Or as DSM suggested: 或DSM建议:
A = numpy.random.uniform(low=0.75, high=1.5, size=(2,3) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.