🚀 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.random()

AI & DATA SCIENCE // np-random-random

np.random.random() returns random floats sampled uniformly from the half-open interval [0.0, 1.0), functionally identical to np.random.rand() but taking a single shape argument (a tuple or integer) instead of separate dimension arguments.

Syntax

np.random.random(size=None)

Deep Dive Course

random() and rand() draw from exactly the same underlying uniform distribution and the same random state, differing only in their calling convention: random() takes size as one argument, following NumPy's usual shape-as-tuple/int pattern, while rand() takes each dimension as a separate positional argument. random() exists partly for consistency with Python's built-in random.random() function, which also returns a single uniform value in [0, 1).

1Understanding np.random.random()

random() and rand() draw from exactly the same underlying uniform distribution and the same random state, differing only in their calling convention: random() takes size as one argument, following NumPy's usual shape-as-tuple/int pattern, while rand() takes each dimension as a separate positional argument. random() exists partly for consistency with Python's built-in random.random() function, which also returns a single uniform value in [0, 1).

💡

random() and rand() are functionally interchangeable in terms of distribution — pick based on which calling convention you prefer, size as a tuple with random(), or dimensions as separate arguments with rand().

editor.html
import numpy as np

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

2Practical Example

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

editor.html
import numpy as np

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

3Best Practices

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

1. Use np.random.random(size) when a shape tuple is more convenient or consistent with surrounding code that uses other shape-taking functions

2. Avoid mixing rand() and random() inconsistently within the same codebase, since they behave identically but read differently

3. Seed explicitly whenever a specific sequence needs to be reproducible for tests or demonstrations

⚠️

Tip: random() and rand() are functionally interchangeable in terms of distribution — pick based on which calling convention you prefer, size as a tuple with random(), or dimensions as separate arguments with rand().

editor.html
import numpy as np

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

Examples

Example 01Basic Usage
import numpy as np

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

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

Best Practices

  • Use np.random.random(size) when a shape tuple is more convenient or consistent with surrounding code that uses other shape-taking functions
  • Avoid mixing rand() and random() inconsistently within the same codebase, since they behave identically but read differently
  • Seed explicitly whenever a specific sequence needs to be reproducible for tests or demonstrations

Interview Question

What's the actual difference between np.random.random() and np.random.rand(), given they sample from the same distribution?

Hint: Think about how each function accepts its shape argument.

Both functions draw uniformly from [0, 1) using the exact same underlying random state and algorithm — there's no statistical difference between them. The only difference is the calling convention: random() accepts a single size argument, which can be an integer or a shape tuple, matching the convention used by most other NumPy array-creation functions, while rand() instead accepts each dimension as a separate positional argument, an older, special-cased style shared with a few of NumPy's other legacy random functions.

Exercises

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

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

Frequently Asked Questions

What's the actual difference between np.random.random() and np.random.rand(), given they sample from the same distribution?

Both functions draw uniformly from [0, 1) using the exact same underlying random state and algorithm — there's no statistical difference between them. The only difference is the calling convention: random() accepts a single size argument, which can be an integer or a shape tuple, matching the convention used by most other NumPy array-creation functions, while rand() instead accepts each dimension as a separate positional argument, an older, special-cased style shared with a few of NumPy's other legacy random functions.

Related Functions

np-random-randnp-random-seednp-random-uniform