Home > Article > Backend Development > How to generate random numbers in numpy
numpy's methods for generating random numbers are: 1. numpy.random.rand(); 2. numpy.random.randn(); 3. numpy.random.randint(); 4. numpy.random. random(); 5. numpy.random.seed().
The operating system for this tutorial: Windows 10 system, Python version 3.11.4, DELL G3 computer.
NumPy is a very powerful Python library for scientific computing and numerical calculations. It provides many functions to generate various types of random numbers. In this answer, I will introduce NumPy in detail Several common methods used to generate random numbers.
1. numpy.random.rand()
This method will generate an array of a given shape. The value of the array is within the interval [0, 1) Uniformly distributed random numbers in the shape of (0, 1). For example, np.random.rand(3, 2) A 3x2 array will be generated, the elements of which are random numbers in the range [0, 1).
import numpy as np random_array = np.random.rand(3, 2) print(random_array)
2. numpy.random.randn()
This function generates an array of a given shape. The values of the array obey the standard normal distribution (the mean is 0, a random number with standard deviation 1). For example np.random.randn(3, 2) A 3x2 array will be generated, the elements of which are random numbers obeying the standard normal distribution.
import numpy as np random_array = np.random.randn(3, 2) print(random_array)
3. numpy.random.randint()
This function generates a random integer within the specified range. You can set the minimum and maximum values of the range and the shape of the array. For example, np.randn.randint(1, 10, (3, 3)) A 3x3 array will be generated, with the elements in the array being random integers from 1 to 9.
import numpy as np random_array = np.random.randint(1, 10, (3, 3)) print(random_array)
4. numpy.random.random()
This function will generate an array of a given shape. The value of the array is in the interval [0, 1) Uniformly distributed random numbers within. Similar to np.random.rand(), This function returns a vectorized version of the function of the random module of the Python standard library. For example, np.random.random((3, 3)) will generate a 3x3 An array of size where the elements are random numbers in the range [0, 1).
import numpy as np random_array = np.random.random((3, 3)) print(random_array)
5, numpy.random.seed()
This function is used to specify the seed when generating pseudo-random numbers. Specifying the same seed will produce the same sequence of random numbers, which is very useful when debugging code. For example, np.random.seed(0) The seed will be set to 0 and the sequence of random numbers generated will be deterministic.
import numpy as np np.random.seed(0) random_array = np.random.rand(3, 3) print(random_array)
These methods are just NumPy One of the many methods provided for generating random numbers. In practical applications, you may use different methods to generate random numbers that conform to a specific distribution or have specific properties. I hope these examples are helpful and give you a better understanding of how to Generate random numbers in NumPy.
The above is the detailed content of How to generate random numbers in numpy. For more information, please follow other related articles on the PHP Chinese website!