Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
How to generate random numbers from a log-normal distribution in Python ?
Next article icon

How to generate random numbers from a log-normal distribution in Python ?

Last Updated : 07 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

A continuous probability distribution of a random variable whose logarithm is usually distributed is known as a log-normal (or lognormal) distribution in probability theory.

A variable x is said to follow a log-normal distribution if and only if the log(x) follows a normal distribution. The PDF is defined as follows.

Probability Density function Log-normal 

Where mu is the population mean & sigma is the standard deviation of the log-normal distribution of a variable. Just like normal distribution which is a manifestation of summation of a large number of Independent and identically distributed random variables, lognormal is the result of multiplying a large number of Independent and identically distributed random variables. Generating a random number from a log-normal distribution is very easy with help of the NumPy library.

Syntax: 

numpy.random.lognormal(mean=0.0, sigma=1.0, size=None)

Parameter:

  • mean: It takes the mean value for the underlying normal distribution.
  • sigma: It takes only non-negative values for the standard deviation for the underlying normal distribution
  • size : It takes either a int or a tuple of given shape. If a single value is passed it returns a single integer as result. If a tuple then it returns a 2D matrix of values from log-normal distribution.

Returns: Drawn samples from the parameterized log-normal distribution(nd Array or a scalar).

The below example depicts how to generate random numbers from a log-normal distribution:

Python3
# import modules import numpy as np import matplotlib.pyplot as plt  # mean and standard deviation mu, sigma = 3., 1.   s = np.random.lognormal(mu, sigma, 10000)  # depict illustration count, bins, ignored = plt.hist(s, 30,                                 density=True,                                  color='green') x = np.linspace(min(bins),                 max(bins), 10000)  pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))        / (x * sigma * np.sqrt(2 * np.pi)))  # assign other attributes plt.plot(x, pdf, color='black') plt.grid() plt.show() 

Output:

Let's prove that log-Normal is a product of independent and identical distributions of a random variable using python. In the program below we are generating 1000 points randomly from a normal distribution and then taking the product of them and finally plotting it to get a log-normal distribution.

Python3
# Importing required modules import numpy as np import matplotlib.pyplot as plt  b = []  # Generating 1000 points from normal distribution. for i in range(1000):     a = 12. + np.random.standard_normal(100)     b.append(np.product(a))  # Making all negative values  into positives b = np.array(b) / np.min(b) count, bins, ignored = plt.hist(b, 100,                                  density=True,                                  color='green')  sigma = np.std(np.log(b)) mu = np.mean(np.log(b))  # Plotting the graph. x = np.linspace(min(bins), max(bins), 10000) pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))        / (x * sigma * np.sqrt(2 * np.pi)))  plt.plot(x, pdf,color='black') plt.grid() plt.show() 

Output:


Next Article
How to generate random numbers from a log-normal distribution in Python ?

A

arun singh
Improve
Article Tags :
  • Python
  • Python-numpy
Practice Tags :
  • python

Similar Reads

    How to Generate Random Numbers from Standard Distributions in R
    Standard Distributions, We have heard this term when working with statistics and data analysis. It is essential to have a technique, for generating numbers that adhere to probability distributions. This allows us to simulate data accurately for tasks like hypothesis testing and Monte Carlo simulatio
    6 min read
    Generate five random numbers from the normal distribution using NumPy
    In Numpy we are provided with the module called random module that allows us to work with random numbers. The random module provides different methods for data distribution. In this article, we have to create an array of specified shape and fill it random numbers or values such that these values are
    2 min read
    How to generate a random number between 0 and 1 in Python ?
    The use of randomness is an important part of the configuration and evaluation of modern algorithms. Here, we will see the various approaches for generating random numbers between 0 ans 1. Method 1: Here, we will use uniform() method which returns the random number between the two specified numbers
    1 min read
    How to Plot a Log Normal Distribution in R
    In this article, we will explore how to plot a log-normal distribution in R, providing you with an understanding of its properties and the practical steps for visualizing it using R Programming Language.Log-Normal DistributionThe log-normal distribution is a probability distribution of a random vari
    5 min read
    Generate Random Numbers From The Uniform Distribution using NumPy
    Random numbers are the numbers that cannot be predicted logically and in Numpy we are provided with the module called random module that allows us to work with random numbers. To generate random numbers from the Uniform distribution we will use random.uniform() method of random module.  Syntax: nump
    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