40

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'])

2 Answers 2

51

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, added source-code to my question that confirms your answer
This is hilarious. What's the reasoning?
@Zapurdead, I would imagine that random and sample were basically alternative names that someone decided to keep. ranf may be a standard name from some other source or community.
What's the difference between these and np.random.rand? (np.random.random is np.random.rand == False)
@Will, one practical difference is the call signature: for array shape s = (2, 3), call random(s) or rand(*s). Without further research, I imagine that's the main difference; the help string for rand says: This is a convenience function. If you want an interface that takes a shape-tuple as the first argument, refer to np.random.random_sample.
0

I got different answers.

print(np.random.random)
print(np.random.ranf)
print(np.random.sample)
print(np.random.rand)
print(np.random.random_sample is np.random.random)
print(np.random.random_sample is np.random.ranf)
print(np.random.random_sample is np.random.sample)


<built-in method random of numpy.random.mtrand.RandomState object at 0x000001FC2C459D08>
<built-in function ranf>
<built-in function sample>
<built-in method rand of numpy.random.mtrand.RandomState object at 0x000001FC2C459D08>
False
False
False

1 Comment

I didn't get the answer as you got. Here's mine, on Python 3.7.3, windows 10, with np.version.full_version == '1.16.4' <built-in method random_sample of mtrand.RandomState object at 0x00000124C7E59F30> <built-in method random_sample of mtrand.RandomState object at 0x00000124C7E59F30> <built-in method random_sample of mtrand.RandomState object at 0x00000124C7E59F30> <built-in method rand of mtrand.RandomState object at 0x00000124C7E59F30> True True True

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.