EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Random Number Generator in R
Secondary Sidebar
R programming Tutorial
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Interview question
    • R Interview Questions

Random Number Generator in R

By Priya PedamkarPriya Pedamkar

Random Number Generator in R

Introduction to Random Number Generator in R

Random Number Generator in R is the mechanism which allows the user to generate random numbers for various applications such as representation of an event taking various values, or samples with random numbers, facilitated by functions such as runif() and set.seed() in R programming that enable the user to generate random numbers and control the generation process, so as to enable the user to leverage the random numbers thus generated in the context of real life problems.

Here is one example below to generate and print 50 values between 1 and 99 using runif() function.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Code

RandomNum <- runif(50, 1, 99)
RandomNum

Output:

Random Number R

A random number generator helps to generate a sequence of digits that can be saved as a function to be used later in operations. Random number generator doesn’t actually produce random values as it requires an initial value called SEED. Random number generation can be controlled with SET.SEED() functions. SET.SEED() command uses an integer to start the random number of generations. Further, the generated random number sequence can be saved and used later.

For example, We will use the code to sample 10 numbers between 1 and 100 and repeat it a couple of times.

For the first time the SET.SEED() will start at seed as 5 and second time as seed as 12. Ten random numbers have been generated for each iteration.

Code:

set.seed(5) # random number will generate from 5
TenRandomNumbers <- sort(sample.int(100, 10))
TenRandomNumbers

Output:

Ten Random

Code:

set.seed(12) # random number will generate from 12
TenRandomNumbers <- sort(sample.int(100, 10))
TenRandomNumbers

Output:

Ten Random No

Random Number Generator Functions

There are in-built functions in R to generate a set of random numbers from standard distributions like normal, uniform, binomial distributions, etc. In the next section we will see different functions like runif(), rnorm(), rbinom() and rexp() to generate random numbers.

1. Uniformly Distributed Random Numbers

To generate uniformly distributed random number runif() is used. Default range 0 – 1. First, we will require to specify the number required to be generated. In addition, the range of the distribution can be specified using the max and min argument.

Code

# To get 5 uniformly distributed Random Numbers
runif(5)

Output:

Uniformly Distributed Random Numbers

Code

# Get 5 random Numbers from 5 to 99
runif(5, min=5, max=99)

Output:

Uniformly Distributed Random Numbers

Code

#To generate 5 integers from 0 to 100
floor(runif(5, min=0, max=101))

Output:

To generate 5 Integer No

Code

# Generating integers without replacement
sample(1:100, 5, replace=FALSE)

Output:

Random number output

2. Normally Distributed Random Numbers

To generate numbers from a normal distribution rnorm() is used. Where mean is 0 and the standard deviation is 1. First, we will require to specify the number required to be generated. In addition, mean and SD (Standard deviation) can be specified arguments.

Code:

rnorm(5)

Output:

 Normally Distributed Random Numbers

Code:

# using a different mean and standard deviation
rnorm(4, mean=70, sd=10)

Output:

Random number output

Code:

# histogram of the numbers to verify the distribution
X <- rnorm(400, mean=70, sd=10)
hist(X)

Output:

Using rnorm() for generating a normal distributed random number

Histogram of X

3. Binomial Random Numbers

The binomial random numbers are a discrete set of random numbers. To derive binomial number value of n is changed to the desired number of trials. For instance trial 5, where n = 5

Code:

n= 5
p=.5
rbinom(1 ,n, p)
# 1 success in 5 trails
n= 5
p=.5
rbinom(19, n, p) # 10 binomial numbers

Output:

image8

4. ExponEntially distributed random numbers

The exponential distribution is used to describe the lifetime of electrical components. For instance, the mean life of an electrical lamp is 1500 hours.

Code:

x=rexp(100, 1/1500)
hist(x, probability=TRUE, col= gray(.9), main="exponential mean=1500")
curve(dexp(x, 1/1500), add= T)

Output:

Random Number - Exponential Mean

Generating Integer And Float Point Number

Now we will learn about generating random numbers for two types of numbers available in R. They are an integer and floating points or float point numbers. R will auto-detect the two categories and move across them as the need arises. An integer in R consists of the whole number that can be positive or negative whereas a floating-point number includes real numbers. It consists of a value that specifies the furthermost digit from the decimal point. The value is in binary and indication is available on the number of binary places to move over. To generate random integers built-in sample() function is reliable and quick. Business needs require you to analyze a sample of data. To select a sample R has sample() function. In order to generate random integers between 5 and 20 below the sample function code is used.

Code:

rn = sample(5:20, 5)
rn

Output:

Generating a random sample of 5

Generating a random sample of 5

In the above example, five values have been generated as the argument stated. We have seen how a subset of random values can be selected in R. In real-time situation you will be required to generate a random sample from an existing data frame. Selecting a sample of data for observation from a large dataset is one of the jobs data engineers undertake in their day to day life.

Code:

Height_Weight_Data <- read.csv("test.csv") # to test this please download csv file
Height_Weight_Data
# Height_Weight_Data sample data frame; selecting a random subset in r
Sample <- Height_Weight_Data[sample(nrow(Height_Weight_Data), 5), ] # pick 5 random rows from dataset
Sample

Output:

Generating random sample from data frame names as Height_Weight_Data

Random Number Generator

Few things to remember regarding floating-point numbers.

  • They are binary in nature.
  • Limited in the real numbers represented.

Now let’s see how random floating number can be generated between -10 to 10

Code:

Random <- runif(n=10, min=-10, max=10)
Random

Output: 

Generating random float point numbers

Generating random float point numbers

Runif() refers to the random uniform. In the above example, we have derived 10 random distributed numbers between [-10:10]

Conclusion

In this article, we have discussed the random number generator in R and have seen how SET.SEED function is used to control the random number generation. We have seen how SEED can be used for reproducible random numbers that are being able to generate a sequence of random numbers and setting up a random number seed generator with SET.SEED(). The statistical method which requires generating random numbers is occasionally used during analysis. R is equipped with multiple functions such as uniform, Normal, Binomial, Poisson, Exponential and Gamma function which allows simulating the most common probability distribution.

Recommended Articles

This has been a guide to Random Number Generator in R. Here we discuss the introduction and functions of Random Number Generator in R along with the appropriate example. You can also go through our other suggested articles to learn more –

  1. Linear Regression in R
  2. Binomial Distribution in R
  3. Logistic Regression in R
  4. Line Graph in R
Popular Course in this category
Statistical Analysis Training (15 Courses, 10+ Projects)
  15 Online Courses |  10 Hands-on Projects |  140+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

R Programming Training (13 Courses, 20+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more