np.random.rand vs np.random.random

PythonNumpy

Python Problem Overview


I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:

np.random.rand

> Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

np.random.random

> Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval.

??? What exactly is the difference there?

Python Solutions


Solution 1 - Python

First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)

Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.

For example, to create an array of samples with shape (3, 5), you can write

sample = np.random.rand(3, 5)

or

sample = np.random.random_sample((3, 5))

(Really, that's it.)


Update

As of version 1.17, NumPy has a new random API. The recommended method for generating samples from the uniform distribution on [0, 1) is:

>>> rng = np.random.default_rng()  # Create a default Generator.
>>> rng.random(size=10)  # Generate 10 samples.
array([0.00416913, 0.31533329, 0.19057857, 0.48732511, 0.40638395,
       0.32165646, 0.02597142, 0.19788567, 0.08142055, 0.15755424])

The new Generator class does not have the rand() or random_sample() methods. There is a uniform() method that allows you to specify the lower and upper bounds of the distribution. E.g.

>>> rng.uniform(1, 2, size=10)
array([1.75573298, 1.79862591, 1.53700962, 1.29183769, 1.16439681,
       1.64413869, 1.7675135 , 1.02121057, 1.37345967, 1.73589452])

The old functions in the numpy.random namespace will continue to work, but they are considered "frozen", with no ongoing development. If you are writing new code, and you don't have to support pre-1.17 versions of numpy, it is recommended that you use the new random API.

Solution 2 - Python

I had the same question. This shows the outputs are identical. The difference is in input format (single arg for dimensions (tuple or list) vs a sequence of dimension args):

# np.random.random([2,3]) vs np.random.rand(2,3)

print()
np.random.seed(1)
print('  .. np.random.random([2,3]):\n', np.random.random([2,3]))
print()
np.random.seed(1)
print('  .. np.random.rand(2,3):\n', np.random.rand(2,3))

# output

 .. np.random.random([2,3]):
 [[4.17022005e-01 7.20324493e-01 1.14374817e-04]
 [3.02332573e-01 1.46755891e-01 9.23385948e-02]]

  .. np.random.rand(2,3):
 [[4.17022005e-01 7.20324493e-01 1.14374817e-04]
 [3.02332573e-01 1.46755891e-01 9.23385948e-02]]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpaceMonkeyView Question on Stackoverflow
Solution 1 - PythonWarren WeckesserView Answer on Stackoverflow
Solution 2 - PythonPeterOView Answer on Stackoverflow