As you probably know, you can generate random numbers in R with the RUNIF() function. However, this function returns numbers with 8 decimals. But, how do you generate random numbers that are rounded to a particular number of decimals, for example, 2?
Creating rounded random numbers in R is a two-step process. First, the RUNIF() draws a random number that contains 8 decimals from a certain range. Then, the ROUND() rounds this random number two a specific number of decimals according to the digits parameter.
Although a combination of the RUNIF() function and the ROUND() function is the most obvious, we also show two other methods that created rounded random numbers.
1. Use the ROUND() Function to Round Random Numbers to 2 Decimal Places
The first option to generate random numbers that contain a specific number of decimals is by using the RUNIF() function and the ROUND() function.
The RUNIF() function has 3 mandatory parameters, namely:
- n: The number of random numbers.
- min: The lower limit of the sample range.
- max: The upper random of the sample range.
runif(n, min, max)
As the image shows, all random numbers have 8 decimals. By adding the ROUND() function to your R code, you can round these numbers to some decimal place. To do so, you only need to specify the digits parameter.
round(runif(n, min, max), digits)
With the R code below we create a random sample of 5 observations between 1 and 10, rounded to 2 decimals.
set.seed(123) x <- round(runif(n = 5, min = 1, max = 10), digits = 2) x
2. Use the SEQ() and SAMPLE() Functions to Round Random Numbers to 2 Decimal Places
The second method that creates rounded random numbers uses the SEQ() function and the SAMPLE() function.
First, the SEQ() function (i.e., sequence) creates a vector of all possible numbers that you can draw. You do so by specifying 3 parameters, namely:
- from: the lower bound of the sample range.
- to: the upper bound of the sample range.
- by: the stepsize of the sample range.
By using by = 1, the SEQ() returns integers. However, if you set by = 0.1, then it returns numbers with one decimal. Likewise, if you set by = 0.01, then it returns numbers with 2 decimals, etc.
seq(from, to, by)
Once you have all the possible numbers, you can use the SAMPLE() function to draw one or more observations. By setting the replace parameter to TRUE, you can draw specific observations more than once.
As an example, with the R code below, we draw 10 observations from a population that contains all numbers between 1.00 and 5.00.
population <- seq(from = 1, to = 10, by = 0.01) x <- sample(population, size = 5, replace = TRUE) x
3. Use the SAMPLE.INT() Function to Round Random Numbers to 2 Decimal Places
The third method to generate random numbers in R that are rounded to 2 decimal places is less straightforward. Still, it’s fun to understand how it works.
Basically, this method first draws random integers from a specific range and then divides these integers to return numbers with decimals. However, to make sure that this method returns random numbers with the specified number of decimals, you need to multiply and divide the original integer by 10number of decimals.
sample.int(max*(10^decimals), size, replace) / (10^decimals)
For example, if you want to draw random numbers between 1 and 10 with 2 decimal places, you need:
- Generate random integers between 100 and 1000 (e.g., 523).
- Divide the random integers by 100 (523 / 100 = 5.23).
You can use the R code below to check if you understand this method.
max = 10 decimals = 2 set.seed(123) x <- sample.int(max*10^decimals, size = 5, replace = TRUE)/(10^decimals) x