runif()

runif() function in R programming language uses uniform distribution to generate random functions. Under this distribution, all the observations are equally likely to occur and it is characterized by a constant probability density function within the range. Uniform Distribution does not have any peak like normal distribution. The values in uniform distribution have equal chances of occurring. It is defined as:

f(x | a, b) = 1/b-a for a ≤ x ≤ b

where,

  • x is a random variable
  • a is the minimum value
  • b is the maximum value

The standard syntax for this function is given by

R




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


where,

  • n: numbers to be generated randomly
  • min: The minimum value of the uniform distribution. Default is 0.
  • max: The maximum value of the uniform distribution. Default is 1.

The min and max values generate the range of the distribution and each value within this range has equal probability of getting selected. We can understand this with the help of a random example and also plot it to understand how this distribution works

R




# Generate 100 random numbers from a uniform distribution between 0 and 10
random_numbers <- runif(100, min = 0, max = 10)
 
# Plot a histogram
hist(random_numbers, breaks = 10, col = "skyblue", main = "Histogram of Random Numbers",
     xlab = "Random Numbers", ylab = "Frequency")


Output:

Difference Between rnorm() and runif() in R

Temperature analysis using runif()

Let us assume we want to perform analysis of temperature of a city recorded over a week. This can help us in planning the outdoor activities or parties, etc.

R




# Set seed for reproducibility
set.seed(123)
 
# Generate random temperatures for each day of the week (in Celsius)
temperatures <- runif(7, min = 10, max = 30)
 
#display the data
head(temperatures)
 
# Define days as a factor to ensure valid finite range
days <- factor(c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
                 "Sunday"))
 
# Create a color palette for gradient colors
colors <- colorRampPalette(c("lightblue", "darkblue"))(length(temperatures))
 
# Plot graph
barplot(temperatures, names.arg = days, col = colors,
        main = "Temperatures Over the Week",
        xlab = "Day", ylab = "Temperature (°C)", border = "white")


Output:

[1] 15.75155 25.76610 18.17954 27.66035 28.80935 10.91113

Difference Between rnorm() and runif() in R

This approach allows us to understand the variability in temperatures throughout the week and can be useful for making informed decisions related to outdoor activities or climate analysis.

Conclusion

In this article, we understood the difference between the rnorm() and runif() functions with the help of different examples and also visualized them to understand better.



The Difference Between rnorm() and runif() in R

While conducting statistical analysis we generate random numbers for reproducibility and experiments. R Programming Language provides two functions runif() and rnorm() to generate random functions. In this article, we will understand the difference between these two functions and how we can use them with the help of examples.

Similar Reads

Difference between rnorm() and runif()

We can understand the difference between the rnorm() and runif() functions in tabulated form as well....

rnorm()

rnorm() is a popular function in R that generates random numbers using normal distribution. Normal distribution, also called Gaussian distribution, represents a bell-shaped curve with the maximum values clustered around the mean value. The Probability Density function of a normal distribution is given by:...

runif()

...