What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to generate Random Numbers using runif() function?

  • Jan 23, 2024
  • 8 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abhisek Ganguly
How to generate Random Numbers using runif() function?

Random number generation is an essential part of statistical programming and data analysis. The R programming language has two commonly used functions for generating random numbers: runif() and rnorm(). In this article, we'll look at the runif() and rnorm() functions. We will also look at how to use the R's runif() function to generate random integers.

What is runif()?

The runif() function in R is designed to generate random numbers from a uniform distribution. Let us look at the syntax of the runif() function.

runif(n, min = 0, max = 1)

 

Here, n represents the number of random values to generate, and min and max represent the range within which the random numbers should be generated. If min and max are not specified, the function will generate random numbers between 0 and 1.

Examples of runif()

Let us now look at some practical examples to see how runif() function works in different scenarios.

1. Generating Default Random Numbers

This is the base use case of the runif() function in R. This doesn't require any attribute and generates a specified amount of random numbers between 0 and 1.

Code:

default_random_numbers <- runif(3)
print(default_random_numbers)

 

Output:

[1] 0.9686412 0.4884955 0.4778220

 

Here, the runif() function is used without specifying min and max, resulting in the generation of 3 random numbers between 0 and 1.

2. Generating Random Numbers in a Specified Range

This is when we want to generate random numbers using the runif() function in a specified range. We can set the min and the max attribute to set the range of the random numbers being generated.

Code:

random_numbers <- runif(5, min = 10, max = 20)
print(random_numbers)

 

Output:

[1] 17.48793 16.67640 10.49416 16.95105 13.63262

 

In this example, the runif() function generates an output containing 5 random numbers between 10 and 20. 

rnorm() - Another Alternative to Generating Random Numbers

While runif() generates random numbers from a uniform distribution, the rnorm() function in R is specifically designed to generate random numbers from a normal (Gaussian) distribution. The syntax for the rnorm() function is as shown.

rnorm(n, mean = 0, sd = 1)

 

Here, n represents the number of random values to generate, the mean is the arithmetic mean of the normal distribution, and sd is the standard deviation. Similar to runif(), if mean and sd are not specified, the function generates random numbers from the standard normal distribution (mean = 0, sd = 1).

To learn more deeply about the rnorm() function in R, read this blog: https://favtutor.com/blogs/rnorm-in-r

Comparing runif() and rnorm()

Both the runif() and the rnorm() functions are used to generate random numbers in R. Although, from a statistical point of view, there's a very big difference between the two. The runif() generates random numbers from a uniform distribution, which means each value within the specified range has an equal probability of being chosen. On the other hand, the rnorm() generates random numbers from a normal distribution, where values near the mean are more likely to be generated.

It is essential that you choose the appropriate function based on the distribution that is consistent with the characteristics of the data you are working with. For example, if you are simulating data with a uniform distribution, runif() is the appropriate choice. If your data follows a normal distribution, rnorm() is more appropriate.

Combining runif() and rnorm()

In some very specific and critical cases, you may need to generate random numbers from both the uniform and normal distributions in the same analysis. Understanding how to use runif() and rnorm() can be useful in such situations.

Code:

random_data <- c(rnorm(80, mean = 50, sd = 10), runif(20, min = 0, max = 100))
print(random_data)

 

In this example, we generate a vector of 100 random numbers. The first 80 numbers follow a normal distribution with a mean of 50 and a standard deviation of 10, while the remaining 20 numbers are generated from a uniform distribution between 0 and 100.

Generating Random Integers with runif()

In some situations, we might need to generate random integers rather than floats. It means we need numbers without the decimal points. In such scenarios we can use the runif() function to generate random integers within a specified range for us.

Let us look at the code on how to do it.

Code:

random_integers <- round(runif(5, min = 1, max = 10))
print(random_integers)

 

Output:

[1] 5 6 8 7 5

 

Here, we generate 5 random numbers using the ruinf() function, within the range of 1 to 10. Then we use the round() function in R programming to round every floating number to their nearest integers. This means that numbers such as 5.48793 gets rounded off to 5 and 5.80952 gets rounded off to 6, because it's nearer to 6. 

Conclusion

Random number generation is a fundamental aspect of statistical analysis and simulation in R programming. The runif() and rnorm() functions are useful for generating random numbers from a uniform and normal distributions, respectively. Having a deep understanding of these functions and how to combine them allows you to efficiently generate simulated data for a variety of analytical and statistical applications. The choice between runif() and rnorm() depends on the underlying distribution of your data, so you need to keep that in mind while writing your R code. Understanding the workings of these functions will help you stimulate your programs and do research as required.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abhisek Ganguly
Passionate machine learning enthusiast with a deep love for computer science, dedicated to pushing the boundaries of AI through academic research and sharing knowledge through teaching.