Python Practice › NumPy Programs › Program 21
Random numbers between 1 and 2
Write a NumPy program to generate random numbers between 1 and 2.
Output
[1.774 1.439 1.859 1.697 1.094]
Approach
rng.uniform(1, 2, size)with a seeded generator.
Solution
import numpy as nprng = np.random.default_rng(42)print(rng.uniform(1, 2, 5).round(3))Output
[1.774 1.439 1.859 1.697 1.094]
Line by line
Line 2
The seed makes the numbers repeatable.
Learn the concept
New to this topic? Read the lesson, then come back.