Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.random.rand() in Python
Next article icon

numpy.random.randn() in Python

Last Updated : 17 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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




# Python Program illustrating
# numpy.random.randn() method
   
import numpy as geek
   
# 1D Array
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




# Python Program illustrating
# numpy.random.randn() method
   
import numpy as geek
   
# 2D Array   
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




# Python Program illustrating
# numpy.random.randn() method
   
import numpy as geek
   
# 3D Array     
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




# Python Program illustrating
# numpy.random.randn() method
   
import numpy as geek
   
# 3D Array     
array = geek.random.randn(2, 2 ,2)
print("3D Array filled with random values : \n", array);
       
# Multiplying values with 3
print("\nArray * 3 : \n", array *3)
  
# Or we cab directly do so by 
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]]]


Next Article
numpy.random.rand() in Python
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Python
  • Python numpy-Random sampling
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • numpy.random.rand() in Python
    This article provides an in-depth exploration of the `numpy.random.rand()` function in Python. It covers the function's syntax, and definition, and includes illustrative examples with detailed explanations for better understanding. numpy.random.rand() Function Syntax The numpy.random.rand() function
    3 min read
  • numpy.random.f() in Python
    With the help of numpy.random.f() method, we can get the random samples of F distribution and return the random samples of numpy array by using this method. Syntax : numpy.random.f(dfnum, dfden, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can see tha
    1 min read
  • numpy.random.wald() in Python
    With the help of numpy.random.wald() method, we can get the random samples from Wald or Inverse Gaussian distribution and return the random samples as numpy array by using this method. Syntax : numpy.random.wald(mean, scale, size=None) Return : Return the random samples as numpy array. Example #1 :
    1 min read
  • numpy.random.zipf() in Python
    With the help of numpy.random.zipf() method, we can get the random samples from zipf distribution and return the random samples as numpy array by using this method. Syntax : numpy.random.zipf(a, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can see tha
    1 min read
  • rand vs normal in Numpy.random in Python
    In this article, we will look into the principal difference between the Numpy.random.rand() method and the Numpy.random.normal() method in detail. About random: For random we are taking .rand() numpy.random.rand(d0, d1, ..., dn) : creates an array of specified shape and fills it with random values.
    3 min read
  • numpy.random.power() in Python
    With the help of numpy.random.power() method, we can get the random samples from power distribution and return the random samples by using this method. Syntax : numpy.random.power(a, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can see that by using n
    1 min read
  • numpy.random.gamma() in Python
    With the help of numpy.random.gamma() method, we can get the random samples of gamma distribution and return the random samples of numpy array by using this method. Syntax : numpy.random.gamma(shape, scale=1.0, size=None) Return : Return the random samples of numpy array. Example #1 : In this exampl
    1 min read
  • numpy.random.standard_t() in Python
    With the help of numpy.random.standard_t() method, we can get the random samples from standard T distribution having degree of freedom and return the random samples by using this method. Syntax : numpy.random.standard_t(df, size=None) # Here df is degree of freedom. Return : Return the random sample
    1 min read
  • numpy.random.triangular() in Python
    With the help of numpy.random.triangular() method, we can get the random samples from triangular distribution from interval [left, right] and return the random samples by using this method. Syntax : numpy.random.triangular(left, mode, right, size=None) Parameters : 1) left - lower limit of the trian
    1 min read
  • numpy.random.rayleigh() in python
    With the help of numpy.random.rayleigh() method, we can get the random samples from Rayleigh distribution and return the random samples. Syntax : numpy.random.rayleigh(scale=1.0, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can see that by using numpy
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences