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.f() in Python
Next article icon

rand vs normal in Numpy.random in Python

Last Updated : 17 Nov, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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.
    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 values.  
  • About normal: For random we are taking .normal()
    numpy.random.normal(loc = 0.0, scale = 1.0, size = None) : creates an array of specified shape and fills it with random values which is actually a part of Normal(Gaussian)Distribution. This is Distribution is also known as Bell Curve because of its characteristics shape.
    Parameters :
      loc   : [float or array_like]Mean of   the distribution.   scale : [float or array_like]Standard   Derivation of the distribution.   size  : [int or int tuples].   Output shape given as (m, n, k) then  m*n*k samples are drawn. If size is   None(by default), then a single value  is returned.   

    Return :

      Array of defined shape, filled with   random values following normal   distribution.  
  • Code 1 : Randomly constructing 1D array




     
     

    Output :

  1D Array filled with random values :    [ 0.84503968  0.61570994  0.7619945   0.34994803  0.40113761]    

Code 2 : Randomly constructing 1D array following Gaussian Distribution




# Python Program illustrating
# numpy.random.normal() method
   
import numpy as geek
   
# 1D Array
array = geek.random.normal(0.0, 1.0, 5)
print("1D Array filled with random values "
      "as per gaussian distribution : \n", array)
# 3D array
array = geek.random.normal(0.0, 1.0, (2, 1, 2))
print("\n\n3D Array filled with random values "
      "as per gaussian distribution : \n", array)
 
 

Output :

  1D Array filled with random values as per gaussian distribution :    [-0.99013172 -1.52521808  0.37955684  0.57859283  1.34336863]    3D Array filled with random values as per gaussian distribution :    [[[-0.0320374   2.14977849]]     [[ 0.3789585   0.17692125]]]  


Code3 : Python Program illustrating graphical representation of random vs normal in NumPy




# Python Program illustrating
# graphical representation of 
# numpy.random.normal() method
# numpy.random.rand() method
   
import numpy as geek
import matplotlib.pyplot as plot
   
# 1D Array as per Gaussian Distribution
mean = 0 
std = 0.1
array = geek.random.normal(0, 0.1, 1000)
print("1D Array filled with random values "
      "as per gaussian distribution : \n", array);
  
# Source Code : 
# https://docs.scipy.org/doc/numpy-1.13.0/reference/
# generated/numpy-random-normal-1.py
count, bins, ignored = plot.hist(array, 30, normed=True)
plot.plot(bins, 1/(std * geek.sqrt(2 * geek.pi)) *
          geek.exp( - (bins - mean)**2 / (2 * std**2) ),
          linewidth=2, color='r')
plot.show()
  
  
# 1D Array constructed Randomly
random_array = geek.random.rand(5)
print("1D Array filled with random values : \n", random_array)
  
plot.plot(random_array)
plot.show()
 
 

Output :

  1D Array filled with random values as per gaussian distribution :    [ 0.12413355  0.01868444  0.08841698 ..., -0.01523021 -0.14621625   -0.09157214]        1D Array filled with random values :    [ 0.72654409  0.26955422  0.19500427  0.37178803  0.10196284]      

Important :
In code 3, plot 1 clearly shows Gaussian Distribution as it is being created from the values generated through random.normal() method thus following Gaussian Distribution.
plot 2 doesn’t follow any distribution as it is being created from random values generated by random.rand() method.

Note :
Code 3 won’t run on online-ID. Please run them on your systems to explore the working.
.



Next Article
numpy.random.f() 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.randn() in Python
    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)
    3 min read
  • numpy.random.standard_normal() in Python
    With the help of numpy.random.standard_normal() method, we can get the random samples from standard normal distribution and return the random samples as numpy array by using this method. Syntax : numpy.random.standard_normal(size=None) Return : Return the random samples as numpy array. Example #1 :
    1 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.laplace() in Python
    With the help of numpy.random.laplace() method, we can get the random samples of Laplace or double exponential distribution having specific mean and scale value and returns the random samples by using this method. Syntax : numpy.random.laplace(loc=0.0, scale=1.0, size=None) Return : Return the rando
    1 min read
  • numpy.random.choice() in Python
    With the help of choice() method, we can get the random samples of one dimensional array and return the random samples of numpy array. Syntax : numpy.random.choice(a, size=None, replace=True, p=None) Parameters: 1) a - 1-D array of numpy having random samples. 2) size - Output shape of random sample
    1 min read
  • numpy.random.vonmises() in Python
    With the help of numpy.random.vonmises() method, we can get the random samples from the von mises distribution and return the random samples as numpy array by using this method. Syntax : numpy.random.vonmises(mu, kappa, size=None) Return : Return the random samples as numpy array. Example #1 : In th
    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.shuffle() in python
    With the help of numpy.random.shuffle() method, we can get the random positioning of different integer values in the numpy array or we can say that all the values in an array will be shuffled randomly. Syntax : numpy.random.shuffle(x) Return : Return the reshuffled numpy array. Example #1 : In this
    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
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