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().
import numpy as np
np.random.seed(0)
print(np.random.random(3))2Practical Example
Here is a real-world application of np.random.random() showing how it is used in production NumPy code.
import numpy as np
np.random.seed(0)
matrix = np.random.random((2, 2))
print(matrix)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().
import numpy as np
np.random.seed(0)
print(np.random.random(3))