Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between various numpy random functions

Tags:

python

numpy

The numpy.random module defines the following 4 functions that all seem to return a float betweeb [0, 1.0) from the continuous uniform distribution. What (if any) is the difference between these functions?

random_sample([size]) Return random floats in the half-open interval [0.0, 1.0).

random([size]) Return random floats in the half-open interval [0.0, 1.0).

ranf([size]) Return random floats in the half-open interval [0.0, 1.0).

sample([size]) Return random floats in the half-open interval [0.0, 1.0).

--------------------------- Edit Follows ---------------------------------------

I found the following in numpy.random source code that supports @askewchan's answer:

# Some aliases: ranf = random = sample = random_sample __all__.extend(['ranf','random','sample']) 
like image 489
Dhara Avatar asked Sep 16 '13 13:09

Dhara


People also ask

What is the difference between NP random rand () and NP random randn ()?

randn generates samples from the normal distribution, while numpy. random. rand from a uniform distribution (in the range [0,1)).

Is NumPy random the same as random?

For this reason, neither numpy. random nor random. random is suitable for any serious cryptographic uses. But because the sequence is so very very long, both are fine for generating random numbers in cases where you aren't worried about people trying to reverse-engineer your data.

What is random function in NumPy?

random() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random floats in the half-open interval [0.0, 1.0). Syntax : numpy.random.random(size=None)

Is NumPy random faster than Python random?

NumPy random for generating an array of random numbers ndarray of 1000 random numbers. The reason why NumPy is fast when used right is that its arrays are extremely efficient. They are like C arrays instead of Python lists.


1 Answers

Nothing.

They're just aliases to random_sample:

In [660]: np.random.random Out[660]: <function random_sample>  In [661]: np.random.ranf Out[661]: <function random_sample>  In [662]: np.random.sample Out[662]: <function random_sample>  In [663]: np.random.random_sample is np.random.random Out[663]: True  In [664]: np.random.random_sample is np.random.ranf Out[664]: True  In [665]: np.random.random_sample is np.random.sample Out[665]: True 
like image 90
askewchan Avatar answered Sep 20 '22 21:09

askewchan