How to generate random float in range with Numpy?

In this Python tutorial, you will learn how to generate random integers in a range with Numpy. We will use the Numpy random uniform method for that purpose.

Numpy random float array

Generating a random float

To generate random float values, just use the random uniform Numpy method. The random uniform syntax is as in the below example.

import numpy as np

my_array = np.random.uniform(-1, 0, 50).reshape(5, -1)
print(f"My array: \n {np.round(my_array, 2)}")

np.random.uniform(-1, 0, 50) generates a random float between -1 and 2. There are 50 of them.

See also  How to Inverse Matrix in Numpy with Python

I reduced decimal points to two by using np.round (my_array, 2).