Categories: Data Science

Learn R – 3 Commands to Generate Random Numbers

This article represents 3 different commands with code examples which could be used to generate random numbers in R programming language. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

  • Runif command
  • Sample command
  • Rnorm command
  • Difference between runif and rnorm command
Runif: Generate Random Numbers based on Uniform Distribution

“Runif” command can be used for generating random numbers based on uniform distribution. One can generate one or more random numbers within a range of numbers. One should note that the random numbers generated using runif commands are all decimal (non-integers) numbers.

# Generate 5 random numbers
runif(5)

# Generate 5 random numbers (Non-integers) between 2 and 7
runif(5, 2, 7)

 

Sample: Generate Random Numbers based on Uniform Distribution

“Sample” command is also used for generating random numbers based on uniform distribution. One can generate one or more random numbers within a range of numbers. One should note that the random numbers generated using runif commands are all integer numbers. This is the key difference between runif and sample command.

# Generate 5 random numbers (Integers)
sample(5)

# Generate 5 random numbers (Integers) between 2 and 7; Duplicates are allowed
# with replace parameter set to True (T)
sample(2:7, 5, replace=T)

# Generate 5 random numbers (Integers) between 2 and 7; Duplicates are NOT allowed
# with replace parameter set to False (F)
sample(2:7, 5, replace=F)

# Generate 5 country names from the vector countryNames with replace as false
countryNames <- c("India","USA","Pakistan","China","Japan","South Korea","Mangolia")
sample(countryNames, 5)

 

Rnorm: Generate Random Numbers based on Normal Distribution

“Rnorm” command is used for generating random numbers based on normal distribution.

# Generate 5 random numbers with mean as 0 and standard deviation as 1
rnorm(5)

# Generate 5 random numbers with mean as 5 and standard deviation as 2
rnorm(500, mean=5, sd=2)

 

Random Number Generation for Uniform & Normal Distribution

The key difference between runif/sample and rnorm command is following:

  • runif/sample command generates the random numbers based on uniform distribution
  • rnorm command generates random numbers based on normal distribution

 

Latest posts by Ajitesh Kumar (see all)
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

2 weeks ago

Agentic AI Design Patterns Examples

In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…

2 weeks ago

List of Agentic AI Resources, Papers, Courses

In this blog, I aim to provide a comprehensive list of valuable resources for learning…

2 weeks ago

Understanding FAR, FRR, and EER in Auth Systems

Have you ever wondered how systems determine whether to grant or deny access, and how…

3 weeks ago

Top 10 Gartner Technology Trends for 2025

What revolutionary technologies and industries will define the future of business in 2025? As we…

3 weeks ago

OpenAI GPT Models in 2024: What’s in it for Data Scientists

For data scientists and machine learning researchers, 2024 has been a landmark year in AI…

3 weeks ago