简体   繁体   English

Python中指定范围内的随机数矩阵

[英]Random number matrix within a specified range in Python

I would like to generate a random number matrix within a specified range, say (0,1) .我想在指定范围内生成一个随机数矩阵,比如(0,1)

import numpy as np
A=np.random.random((3, 3))
print("A =",[A])

numpy.random.uniform can receive low , high and size parameters numpy.random.uniform可以接收low , highsize参数

Range 0-1 is the default范围 0-1 是默认值

A = np.random.uniform(size=(3, 3))
print("A =", [A])

Output Output

A = [array([[0.76679099, 0.57459256, 0.07952816],
            [0.02736909, 0.05905416, 0.09909474],
            [0.08690106, 0.81983883, 0.18740471]])]

If you want another range specify it with parameters如果您想要另一个范围,请使用参数指定它

A = np.random.uniform(0.2, 0.3, (3, 3))
print("A =", [A]) 

Output Output

A = [array([[0.20548205, 0.25373507, 0.28957419],
            [0.20496673, 0.27004844, 0.28633947],
            [0.22325187, 0.26327935, 0.24548129]])]

you will be use it generate range [0, 1)您将使用它生成范围 [0, 1)

np.random.rand(d0, d1, ..., dn)

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).创建一个给定形状的数组,并用 [0, 1) 上均匀分布的随机样本填充它。

https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html

You can use Numpy's built-in rand method, which generates a matrix with random numbers with a uniform distribution over [0, 1).您可以使用 Numpy 的内置 rand 方法,该方法生成一个矩阵,其中包含在 [0, 1) 上均匀分布的随机数。

A = np.random.rand(3, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM