🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.random.rand()

AI & DATA SCIENCE // np-random-rand

np.random.rand() generates an array of a given shape filled with random floats sampled uniformly from the half-open interval [0, 1).

Syntax

np.random.rand(d0, d1, ...)

Deep Dive Course

Unlike most NumPy functions that take a shape as a single tuple argument, rand() takes each dimension as a separate positional argument, e.g. np.random.rand(2, 3) for a 2x3 array, rather than passing an actual tuple, which raises a TypeError — a common gotcha for people used to NumPy's usual shape-as-tuple convention. Every value is drawn independently and uniformly across [0, 1), meaning every value in that range is equally likely to be sampled.

1Understanding np.random.rand()

Unlike most NumPy functions that take a shape as a single tuple argument, rand() takes each dimension as a separate positional argument, e.g. np.random.rand(2, 3) for a 2x3 array, rather than passing an actual tuple, which raises a TypeError — a common gotcha for people used to NumPy's usual shape-as-tuple convention. Every value is drawn independently and uniformly across [0, 1), meaning every value in that range is equally likely to be sampled.

💡

rand() takes dimensions as separate arguments, not a shape tuple — np.random.rand(2, 3) works, but passing an actual tuple raises a TypeError; this is inconsistent with most other NumPy array-creation functions and easy to trip over.

editor.html
import numpy as np

np.random.seed(0)
print(np.random.rand(3))
localhost:3000

2Practical Example

Here is a real-world application of np.random.rand() showing how it is used in production NumPy code.

editor.html
import numpy as np

np.random.seed(0)
matrix = np.random.rand(2, 3)
print(matrix)
localhost:3000

3Best Practices

Follow these guidelines when working with np.random.rand():

1. Pass dimensions as separate arguments to rand(), not a tuple, since it's an exception to NumPy's usual shape-as-tuple convention

2. Prefer the newer Generator API (np.random.default_rng()) over the legacy np.random.rand() in new code, for better statistical properties and explicit random state management

3. Set a seed (via np.random.seed() or a Generator's seed argument) whenever reproducibility matters, such as in tests or tutorials

⚠️

Tip: rand() takes dimensions as separate arguments, not a shape tuple — np.random.rand(2, 3) works, but passing an actual tuple raises a TypeError; this is inconsistent with most other NumPy array-creation functions and easy to trip over.

editor.html
import numpy as np

np.random.seed(0)
print(np.random.rand(3))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(0)
print(np.random.rand(3))
Example 02Advanced Example
import numpy as np

np.random.seed(0)
matrix = np.random.rand(2, 3)
print(matrix)

Best Practices

  • Pass dimensions as separate arguments to rand(), not a tuple, since it's an exception to NumPy's usual shape-as-tuple convention
  • Prefer the newer Generator API (np.random.default_rng()) over the legacy np.random.rand() in new code, for better statistical properties and explicit random state management
  • Set a seed (via np.random.seed() or a Generator's seed argument) whenever reproducibility matters, such as in tests or tutorials

Interview Question

Why does np.random.rand(2, 3) work, but passing an actual tuple to rand() raise a TypeError?

Hint: Think about how rand()'s arguments differ from most other NumPy array-creation functions.

Most NumPy array-creation functions, like np.zeros() or np.ones(), accept a single shape argument that can be a tuple. rand() is an older, special-cased function that instead expects each dimension as a separate positional argument, following the same style as some of NumPy's other legacy random functions. Passing an actual tuple as a single argument doesn't match what rand() expects for any individual dimension, so it raises a TypeError rather than being interpreted as a shape.

Exercises

MediumPractice using np.random.rand() in a real scenario.
View Solution
import numpy as np

np.random.seed(0)
print(np.random.rand(3))

Frequently Asked Questions

Why does np.random.rand(2, 3) work, but passing an actual tuple to rand() raise a TypeError?

Most NumPy array-creation functions, like np.zeros() or np.ones(), accept a single shape argument that can be a tuple. rand() is an older, special-cased function that instead expects each dimension as a separate positional argument, following the same style as some of NumPy's other legacy random functions. Passing an actual tuple as a single argument doesn't match what rand() expects for any individual dimension, so it raises a TypeError rather than being interpreted as a shape.

Related Functions

np-random-randnnp-random-seednp-random-uniform