The numpy.random.randn() function creates an array of specified shape and fills it with random values as per standard normal distribution.
If positive arguments are provided, randn generates an array of shape (d0, d1, …, dn), filled with random floats sampled from a univariate “normal” (Gaussian) distribution of mean 0 and variance 1 (if any of the d_i are floats, they are first converted to integers by truncation). A single float randomly sampled from the distribution is returned if no argument is provided.
Syntax :
numpy.random.randn(d0, d1, ..., dn)
Parameters :
d0, d1, ..., dn : [int, optional]Dimension of the returned array we require,
If no argument is given a single Python float is returned.
Return :
Array of defined shape, filled with random floating-point samples from
the standard normal distribution.
Code 1 : randomly constructing 1D array
Python3
import numpy as geek
array = geek.random.randn( 5 )
print (" 1D Array filled with random values : \n", array);
|
Output :
1D Array filled with random values :
[-0.51733692 0.48813676 -0.88147002 1.12901958 0.68026197]
Code 2 : randomly constructing 2D array
Python3
import numpy as geek
array = geek.random.randn( 3 , 4 )
print (" 2D Array filled with random values : \n", array);
|
Output :
2D Array filled with random values :
[[ 1.33262386 -0.88922967 -0.07056098 0.27340112]
[ 1.00664965 -0.68443807 0.43801295 -0.35874714]
[-0.19289416 -0.42746963 -1.80435223 0.02751727]]
Code 3 : randomly constructing 3D array
Python3
import numpy as geek
array = geek.random.randn( 2 , 2 , 2 )
print (" 3D Array filled with random values : \n", array);
|
Output :
3D Array filled with random values :
[[[-0.00416587 -0.66211158]
[-0.97254293 -0.68981333]]
[[-0.18304476 -0.8371425 ]
[ 2.18985366 -0.9740637 ]]]
Code 4 : Manipulations with randomly created array
Python3
import numpy as geek
array = geek.random.randn( 2 , 2 , 2 )
print (" 3D Array filled with random values : \n", array);
print ("\nArray * 3 : \n", array * 3 )
array = geek.random.randn( 2 , 2 , 2 ) * 3 + 2
print ("\nArray * 3 + 2 : \n", array);
|
Output :
3D Array filled with random values :
[[[ 1.9609643 -1.89882763]
[ 0.52252173 0.08159455]]
[[-0.6060213 -0.86759247]
[ 0.53870235 -0.77388125]]]
Array * 3 :
[[[ 5.88289289 -5.69648288]
[ 1.56756519 0.24478366]]
[[-1.81806391 -2.6027774 ]
[ 1.61610704 -2.32164376]]]
Array * 3 + 2 :
[[[-2.73766306 6.80761741]
[-1.57909191 -1.64195796]]
[[ 0.51019498 1.30017345]
[ 3.8107863 -4.07438963]]]
References :
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.randn.html
Note :
These codes won’t run on online IDE’s. Please run them on your systems to explore the working.
.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Mar, 2022
Like Article
Save Article