Fundamentals 7 min read

Generating Random Numbers with Python’s random Module and NumPy

This article explains how to use Python’s built‑in random module and NumPy’s random submodule to generate various types of pseudorandom numbers, covering functions such as random(), randint(), randrange(), uniform(), choice(), shuffle(), sample() and their NumPy equivalents with code examples and seed control.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Generating Random Numbers with Python’s random Module and NumPy

Random numbers are widely used in applications such as password salting and Monte Carlo simulations. Python provides the built‑in random module for generating pseudorandom values, while NumPy offers a powerful numpy.random submodule for multidimensional arrays.

Python random module methods:

random.random() – returns a float in the half‑open interval [0.0, 1.0). Example: print("random: ", random.random())

random.randint(a, b) – returns an integer N such that a ≤ N ≤ b. Example: print("randint: ", random.randint(6,8))

random.randrange(start, stop, step) – returns a randomly selected element from range(start, stop, step). Example: print("randrange: ", random.randrange(20,100,5))

random.uniform(a, b) – returns a float N such that a ≤ N ≤ b. Example: print("uniform: ", random.uniform(5,10))

random.choice(seq) – picks a random element from a non‑empty sequence. Example: print("choice: ", random.choice("www.yuanxiao.net"))

random.shuffle(seq) – shuffles a mutable sequence in place. Example: num = [1, 2, 3, 4, 5] random.shuffle(num) print("shuffle: ", num)

random.sample(seq, n) – returns a list of n unique elements chosen from the sequence. Example: num = [1, 2, 3, 4, 5] print("sample: ", random.sample(num, 3))

All these functions rely on a seed value; calling random.seed(x) makes the sequence reproducible, while omitting it uses the current system time.

NumPy random submodule methods:

numpy.random.rand(d0, d1, …, dn) – generates an array of shape (d0, d1, …, dn) with values in [0, 1). Example: print("np.random.rand:\n", np.random.rand(4,2))

numpy.random.randn(d0, d1, …, dn) – returns samples from the standard normal distribution. Example: print("np.random.randn:\n", np.random.randn(2,4))

numpy.random.randint(low, high=None, size=None, dtype='l') – returns random integers from low (inclusive) to high (exclusive). Example: print("np.random.randint:\n", np.random.randint(1, size=5))

numpy.random.seed() – sets the seed for reproducible random numbers.

Both modules generate pseudorandom numbers based on deterministic algorithms; using the same seed reproduces the same sequence, while varying the seed (or omitting it) yields different results each run.

For further learning, the article also provides QR‑code links to free Python courses and additional resources.

TutorialNumPyRandompseudorandom
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.