How to generate random numbers using numpy?

Random numbers can be generated using numpy through the following methods:

  1. Return a random number between 0 and 1 using NumPy.
import numpy as np

random_array = np.random.rand(2, 3)
print(random_array)

Output:

[[0.12345678 0.45678901 0.98765432]
 [0.23456789 0.56789012 0.87654321]]
  1. Generate random numbers from a standard normal distribution using numpy.
import numpy as np

random_array = np.random.randn(2, 3)
print(random_array)

Output:

[[-0.12345678  0.45678901 -0.98765432]
 [ 0.23456789 -0.56789012  0.87654321]]
  1. Generate a random integer using numpy.
import numpy as np

random_array = np.random.randint(0, 10, size=(2, 3))
print(random_array)

Output:

[[2 6 4]
 [1 8 3]]

Here are some common methods for generating random numbers, and there are other methods available to generate random numbers with different distributions. You can choose the appropriate method based on specific needs.

bannerAds