# Np.random.rand() vs np.random.randn()

**URL:** <https://community.deeplearning.ai/t/np-random-rand-vs-np-random-randn/1972>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** coursera-platform\
**Created:** [April 27, 2021, 1:15pm UTC](https://community.deeplearning.ai/t/np-random-rand-vs-np-random-randn/1972 "2021-04-27T13:15:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaskeerat](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jaskeerat](https://community.deeplearning.ai/u/Jaskeerat)\
**Post date:** [April 27, 2021, 1:15pm UTC](https://community.deeplearning.ai/t/np-random-rand-vs-np-random-randn/1972/1 "2021-04-27T13:15:12Z")

</div>

Hey, so in all previous programming assignments, I have been using np.random.randn(shape), however in the gradient checking programming assignment, when I used np.random.randn() out of memory, the test didn’t pass. I then read carefully to find it asked us to use np.random.rand(),  
What’s the difference?

Is it just that the test didn’t pass because it was configured with np.random.rand() but in real life for use, np.random.randn() will work just as well, or am I missing something?

Thanks!

---

<div class="post-metadata">

**Author:** ![javier](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/javier/32/47_2.png) [@javier](https://community.deeplearning.ai/u/javier)\
**Post date:** [April 27, 2021, 2:51pm UTC](https://community.deeplearning.ai/t/np-random-rand-vs-np-random-randn/1972/2 "2021-04-27T14:51:14Z")

</div>

Hi @Jaskeerat, the main difference between both functions is the distribution of the generated random numbers:  
[numpy.random.rand()](https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html) produces numbers in a uniform distribution

 ![rand](https://global.discourse-cdn.com/dlai/original/1X/ad4fb0227d93348b534993e75c93571ba5b8cc21.png)  
and [numpy.random.randn()](https://numpy.org/doc/stable/reference/random/generated/numpy.random.randn.html) produces numbers in a normal distribution  
 ![randn](https://global.discourse-cdn.com/dlai/original/1X/7886520702019352d31ba834146d7effb6649d6f.png)  
When used for weight initialization, randn() helps most the weights to Avoid being close to the extremes.  
An intuitive way to see it is, for example, if you take the sigmoid() function. You’ll remember that the slope near 0 or near 1 is extremely small, so the weight near those extremes will converge much more slowly to the solution.  
 ![sigmoid](https://global.discourse-cdn.com/dlai/original/1X/dd876fc1a4793796c3569b25a4ac5874bbd8c68e.jpeg)  
Do you refer to the Regularization exercise? the Gradient checking exercise does not require using a random function.  
If you refer to the Regularization exercise, there the rand() function is being used to create the Dropout matrix, and not for weight initialization (the first time I did the exercise, I also erroneusly used randn() 😉)

---

<div class="post-metadata">

**Author:** ![Muskan\_Mahajan](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Muskan\_Mahajan](https://community.deeplearning.ai/u/Muskan_Mahajan)\
**Post date:** [December 4, 2021, 7:43am UTC](https://community.deeplearning.ai/t/np-random-rand-vs-np-random-randn/1972/3 "2021-12-04T07:43:22Z")

</div>

Hi Javier!  
I am totally clear on the importance of randn, thank you for the explainer!

Just 1 doubt: You mentioned that “You’ll remember that the slope near 0 or near 1 is extremely small”.

However, isn’t the slope maximum at 0?

---

<div class="post-metadata">

**Author:** ![xtianhb](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/xtianhb/32/14458_2.png) [@xtianhb](https://community.deeplearning.ai/u/xtianhb)\
**Post date:** [February 24, 2022, 10:21am UTC](https://community.deeplearning.ai/t/np-random-rand-vs-np-random-randn/1972/4 "2022-02-24T10:21:44Z")

</div>

This helped me, I was using the wrong function for course 2, week 1, dropout ex 6.1.  
Thanks
