Python Practice › NumPy Programs › Program 21

Random numbers between 1 and 2

EasyNumPy Programs · 21 of 100

Write a NumPy program to generate random numbers between 1 and 2.

Advertisement
Output
[1.774 1.439 1.859 1.697 1.094]

Approach

  1. rng.uniform(1, 2, size) with a seeded generator.

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment