# How to use numpy.random.uniform() in python.

The np.random.uniform() function is used to create an array with random samples from a uniform probability distribution of given low and high values.

### random.uniform(low=0.0, high=1.0, size=None)

• Purpose: The numpy random uniform function used for creating a numpy array with random float values from low to high interval.

• Parameteres:

• Low: float or array-like of floats,optional: Lowest integers are drawn from the random values.
• High: floats or array-like of floats: Larger or highest integers are drawn from the random values.
• Size: int or tuple of ints, optional: The shape of the array you want to create.
• Returns: out:int or ndarray of ints Returns an array with specified shape along with random float values in it.

# Import Packages
import numpy as np
import warnings

warnings.filterwarnings("ignore")


## 1.Construct 1-D array using uniform() function

In this example you will understand how to construct a 1-D array using np.random.uniform() function

### Example 1: Create a 1-D array using uniform()

# create a 1-D array
a = np.random.uniform(low = 2.0, size = 5)

# print the newly created array
print(a)

[1.60417407 1.37901136 1.3266928  1.44675961 1.45849329]

• when low = 2.0 then uniform() takes float between 1 to 2.
• The size = 5 which creates a 1-D array
• Now a 1-D array is created with float values as 1.9,1.6 etc.

### Example 2: Create a 1-D array using uniform()

# create a 1-D array
a = np.random.uniform(low = 1.0, high = 6.0, size = 8)

# print the newly created array
print(a)

[5.37619456 3.9567411  3.12774544 2.69062294 3.08278497 4.36318074
5.09281948 3.56006094]

• when low = 1.0 and high = 6.0 uniform() takes float values between 1 to 5.
• The size = 8 which creates a 1-D array
• Now a 1-D array is created with float values as 1.8, 5.0,3.4.. etc.

## 2.Construct 2-D array using uniform() function

In this example you will understand how to construct a 2-D array using np.random.uniform() function

### Example 1: Create a 2-D array using uniform()

# create a 2-D array
a = np.random.uniform(low = 2.0, size = (2,3))

# print newly created array
print(a)

[[1.69158727 1.00450782 1.00276849]
[1.60687721 1.5502569  1.86473745]]

• when low = 2.0 then uniform() takes float between 1 to 2.
• The size = (2,3) which creates a 2-D array
• Now a 2-D array is created with float values as 1.05,1.6 etc.

### Example 2: Create a 2-D array using uniform()

# create a 2-D array
a = np.random.uniform(low = 0.0, high = 7.0, size = (3,4))

# print newly created array
print(a)

[[0.78990561 6.85819363 4.10012043 3.8383489 ]
[5.71356472 0.04311822 4.06363112 3.89891944]
[0.1604924  4.95278104 3.47837889 5.21118188]]

• when low = 0.0 and high = 7.0 uniform() takes float values between 0 to 6.
• The size = (3,4) which creates a 2-D array
• Now a 2-D array is created with float values as 5.6, 4.6,3.9.. etc.

## 3.Construct 3-D array using uniform() function

In this example you will understand how to construct a 3-D array using np.random.uniform() function

# create a 3-D array
a = np.random.uniform(low = 0.0, high = 5.0, size = (2,3,4))

# print newly created array
print(a)

[[[1.74555641e+00 1.40071802e+00 4.80421736e-04 3.94613858e+00]
[3.44925015e+00 4.71720306e+00 3.47847746e+00 3.06168182e+00]
[8.31717384e-02 3.15805870e+00 3.99576359e+00 2.37585483e+00]]

[[4.38556648e-01 8.77964361e-01 2.31830087e+00 2.83192855e+00]
[2.18488359e+00 3.94012301e+00 2.59823334e+00 2.71837063e-01]
[4.15053227e+00 2.31602833e+00 3.06735877e+00 3.23441499e+00]]]


Note When creating a 3-D array you have to specify both high and low parameters. If you specify only low parameter it returns a error.

Try the above scenario to know what kind of error it returns.

• When is set to low = 0.0 and high = 5.0 the uniform() takes floats values between 0 to 4.
• As you set the size as (2,3,4) which creates a 3-D array. with two sub arrays, three rows and four columns for each sub array.
• Now the 3-D array is created with integers between 0.7,4.04,1.35.. etc.

Q1: Can you create a 3-D arrays without defining high parameter?
Ans: No, you cannot create a 3-D array without defining a high parameter. That returns a value error.

Course Preview