Menu

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

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

Written by Selva Prabhakaran | 3 min read

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.

python
# 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()

python
# create a 1-D array
a = np.random.uniform(low = 2.0, size = 5)
python
# print the newly created array
print(a)
python
[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()

python
# create a 1-D array
a = np.random.uniform(low = 1.0, high = 6.0, size = 8)
python
# print the newly created array
print(a)
python
[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()

python
# create a 2-D array
a = np.random.uniform(low = 2.0, size = (2,3))
python
# print newly created array
print(a)
python
[[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()

python
# create a 2-D array
a = np.random.uniform(low = 0.0, high = 7.0, size = (3,4))
python
# print newly created array
print(a)
python
[[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

python
# create a 3-D array
a = np.random.uniform(low = 0.0, high = 5.0, size = (2,3,4))
python
# print newly created array
print(a)
python
[[[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.

4.Test your knowledge

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.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master Python — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Free Callback - Limited Slots
Not Sure Which Course to Start With?
Talk to our AI Counsellors and Practitioners. We'll help you clear all your questions for your background and goals, bridging the gap between your current skills and a career in AI.
10-digit mobile number
📞
Thank You!
We'll Call You Soon!
Our learning advisor will reach out within 24 hours.
(Check your inbox too — we've sent a confirmation)
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science