How to generate random numbers with precision in NumPy array

You can generate random numbers with precision in the NumPy array with the following code. If you want to learn Python, I highly recommend reading This Book.

# Import the NumPy library as np
import numpy as np

# Generate Random Numbers
a = np.random.randn(5, 3)

# Display random numbers
print(a)

# Apply precision
np.set_printoptions(precision=3)

# Display random numbers with precision
print(a)

Output:

[[ 0.3290993   1.30199764  0.52349445]
 [-0.33436595 -0.8980697  -1.2117141 ]
 [ 0.79281516  0.51044136  0.52811383]
 [ 0.18019728 -1.00861312 -0.02883181]
 [ 0.14929703 -0.64397528 -0.60102909]]
[[ 0.329  1.302  0.523]
 [-0.334 -0.898 -1.212]
 [ 0.793  0.51   0.528]
 [ 0.18  -1.009 -0.029]
 [ 0.149 -0.644 -0.601]]

Free Learning Resources

Leave a Comment

Your email address will not be published.