How can I use the runif function in R for generating random numbers? 2

How can I use the runif function in R for generating random numbers?

The runif function in R is a built-in function that allows users to generate random numbers within a specified range. This function takes in two parameters, “n” which represents the number of random numbers to be generated and “min” and “max” which represent the lower and upper bounds of the range. By utilizing the runif function, users can easily generate a set of random numbers for various statistical and computational purposes. This function is particularly useful for tasks such as simulation, testing, and sampling. The random numbers generated by the runif function are uniformly distributed, meaning they have an equal chance of being selected within the specified range. Overall, the runif function provides a convenient and efficient way for users to generate random numbers in R.

Use runif Function in R (4 Examples)


You can use the runif() function to generate random values from a in R.

This function uses the following syntax:

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

where:

  • n: The number of random values to generate
  • min: The minimum value of the distribution (default is 0)
  • max: The maximum value of the distribution (default is 1)

The following examples show how to use the runif() function in different scenarios.

Example 1: Use runif() to Generate Random Values

The following code shows how to use the runif() function to generate 10 random values from a uniform distribution that ranges from 50 to 100:

#make this example reproducible
set.seed(5)

#generate 10 random values from uniform distribution
runif(n=10, min=50, max=100)

[1] 60.01072 84.26093 95.84379 64.21997 55.23251 85.05287 76.39800 90.39676
[9] 97.82501 55.52265

Note that each of the 10 random values generated is between 50 and 100.

Example 2: Use runif() to Generate Random Values Rounded to Decimal Place

The following code shows how to use the round() function with the runif() function to generate 10 random values from a uniform distribution that ranges from 50 to 100, in which each value is rounded to one decimal place:

#make this example reproducible
set.seed(5)

#generate 10 random values from uniform distribution rounded to one decimal place
round(runif(n=10, min=50, max=100), 1)

[1] 63.7 74.5 65.9 78.0 63.1 60.1 69.4 94.4 77.7 92.1

Note that each of the 10 random values generated is between 50 and 100 and is rounded to one decimal place.

Example 3: Use runif() to Generate Random Values Rounded to Whole Numbers

The following code shows how to use the round() function with the runif() function to generate 10 random values from a uniform distribution that ranges from 50 to 100, in which each value is rounded to a whole number:

#make this example reproducible
set.seed(5)

#generate 10 random values from uniform distribution rounded to whole number
round(runif(n=10, min=50, max=100), 0)

[1] 64 75 66 78 63 60 69 94 78 92

Example 4: Use runif() to Create Histogram of Uniform Distribution

The following code shows how to use the runif() function to generate 1,000 random values from a uniform distribution that ranges from 50 to 100, and then use the hist() function to create a histogram that visualizes this distribution of values.

#make this example reproducible
set.seed(5)

#generate 1,000 random values from uniform distribution
values <- runif(n=1000, min=50, max=100)

#generate histogram to visualize these values
hist(values)

runif histogram in R

The result is a histogram that displays the distribution of the 1,000 values generated from the uniform distribution.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

Cite this article

stats writer (2024). How can I use the runif function in R for generating random numbers?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-runif-function-in-r-for-generating-random-numbers/

stats writer. "How can I use the runif function in R for generating random numbers?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-runif-function-in-r-for-generating-random-numbers/.

stats writer. "How can I use the runif function in R for generating random numbers?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-runif-function-in-r-for-generating-random-numbers/.

stats writer (2024) 'How can I use the runif function in R for generating random numbers?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-runif-function-in-r-for-generating-random-numbers/.

[1] stats writer, "How can I use the runif function in R for generating random numbers?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use the runif function in R for generating random numbers?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top