NumPy (C1_W2_Lab01_Python_Numpy_Vectorization_Soln)

What is the difference between:
(np.random.random_sample) and (np.random.rand)

Hey @Mohamed_Mostafa3,
A look at the documentation of numpy.random.rand is all that you need. In the docs, it is mentioned that:

This is a convenience function for users porting code from Matlab, and wraps random_sample. That function takes a tuple to specify the size of the output, which is consistent with other NumPy functions like numpy.zeros and numpy.ones.

In simple words, rand is simply a wrapper for the random_sample function, and the only difference is how you pass the size. rand takes the size as separate argument for each dimension, while random_sample takes the size as a tuple.

In your example, you have passed a single dimension as the size, and hence, no difference is there. Try passing two dimensions as the size, and you will realise the difference yourself. Let us know if this helps.


1 Like