How to create a normal distribution in R?

This recipe helps you create a normal distribution in R

Recipe Objective

Random numbers are generated in quite a few cases in statistics to carry out sampling and simulation. Mostly, a data scientist is in a need of a set of random numbers which are mostly taken from two types of distribution: ​

  1. Uniform distribution
  2. Normal distribultion

These random numbers generated mimic the properties of uniform or normal distribution in a certain interval. ​

In this recipe, you will learn how to create a random normal distribution.. ​

Normal distribution is a type of probability distribution which looks like a bell with co-inciding median, mode and mean. In this type of distribution, 68% observations are within 1 standard deviation from it's mean. ​

Note: Whenever we are generating random numbers, you are using an algorithm that requires a seed whose function is to initialise. These numbers are actually pseudorandom numbers which can be predicted if we know the seed and the generator. Setting a seed means iniltialising a pseudorandom generator. We set a seed when we need the same output of numbers everytime you want to generate random numbers. If we don't set a seed, the generated pseudorandom numbers are different on each execution. ​

Example:Creating a normal distribution by generating 100 random numbers from a normal distribution by seeting a seed

We use rnorm() function to carry out this task. ​

Syntax: rnorm(n, min = , max = ) ​

where: ​

  1. n = size of the distribution
  2. min, max = specifies the interval in which you would like the distribution to be

Additionally, use set.seed() function to set a seed. We specify any integer in the function as a seed. ​

# setting a seed set.seed(20) # using random numbers from normal distribution between 1 and 30 random_dist = rnorm(10000, mean = 0, sd = 1) #plotting a histogram of the generated numbers using hist() function hist(random_dist, breaks = 100)

Note: ​

  1. The distribution remains constant even after multiple execution.
  2. You can see that the mean, mode and median co-incides in the above plot indicating a normal distribution

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.