R rnorm() Function

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

In statistical research, random numbers are crucial because they allow us to represent inconsistent values and variability in real-world data. We can develop synthetic data that looks like actual data to assess statistical methodologies and validate model robustness. The rnorm() function in R is a powerful built-in function used for generating random numbers.

In this article, we will explore the R rnorm() function to produce normally distributed random numbers, control the amount of generated observations, define the mean and standard deviation of the generated data, and input customized mean values to the rnorm() function.

What is the rnorm() function?

The R function rnorm() is used for generating a vector of random numbers with a normal distribution. This distribution is useful in statistical research due to its bell-shaped curve. The primary goal of the rnorm() function is to generate data points that mimic the typical patterns observed in real-world events. These typical patterns are common behaviors and trends we observe in the real world, such as most students scoring around the class mean score on a test. Likewise, we can see that most days have temperatures close to the average.

Sharpen Your Fundamentals with Free Learning

Syntax

To use the rnorm() function in R, we can use the following syntax:

In this syntax of the rnorm() function, the different parameters are:

  • n represents the number of random values to generate.
  • mean signifies the mean (average) of the distribution, with a default of 0.
  • sd denotes the standard deviation, indicating the spread of values, with a default of 1.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

How to Use rnorm() Function in R?

To use the rnorm() function in R, we have to specify the number of observations we want to generate by passing the value for the n parameter. We can also specify the mean and standard deviation of the normal distribution by passing values for the mean and sd parameters, respectively. If these parameters are not specified, their default values will be used.

R rnorm() Examples

To use the rnorm() function in R effectively, let us understand how to customize the random number generation according to our specific requirements.

  1. Controlling the Number of Observations:

    The main parameter we need to set when using the rnorm() function is n, which stands for the number of observations or random numbers we want to generate. When we specify this value, we fix the size of the dataset that we'll be working with.

    For example, let us generate 25 random values using the following code:

    Output:

  2. Specifying the Distribution Characteristics:

    The two additional parameters mean and sd, allow us to customize the characteristics of the normal distribution from which the random numbers are drawn. The mean parameter controls the average of the distribution whereas the sd parameter determines the spread of the distribution.

    Output:

    In addition to specifying the mean and standard deviation, we can also control the seed of the random number generator using the set.seed() function. This allows us to generate reproducible random numbers.

    For example, if we want to generate the same sequence of random numbers every time we run our code, we can use the following code:

    Output:

  3. Generating Normalized Values with a Custom Mean:

    We can use the rnorm() function to create a dataset of random values that not only follow a normal distribution but also have their mean adjusted to a predefined value.

    For Example:

    Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • The rnorm() function in R is a useful tool for generating random numbers that simulate real-world uncertainty, which helps with statistical analysis.
  • With its ability to produce normally distributed data, rnorm() allows us to model a variety of statistical scenarios and verify model robustness.
  • It is possible to match specific needs by adjusting parameters like n, mean, and sd to fine-tune the characteristics of the generated data.
  • The rnorm() function finds use in many real-world scenarios such as simulating data for hypothesis testing, predicting stock prices in finance, generating synthetic patient data for medical trials, simulating weather conditions to evaluate the accuracy of weather prediction models, and modeling consumer behavior for market research, etc.