Open In App

How to Generate Random Numbers from Standard Distributions in R

Standard Distributions, We have heard this term when working with statistics and data analysis. It is essential to have a technique, for generating numbers that adhere to probability distributions. This allows us to simulate data accurately for tasks like hypothesis testing and Monte Carlo simulations. R Programming Language offers built-in functions for generating random numbers from various standard distributions.

1. Probability Distributions

These mathematical descriptions help us understand how random variables are distributed. Standard distributions like the Gaussian) Uniform and exponential have defined properties.



2. Random Number Generation

R provides functions that generate numbers following specific distributions. These functions utilize algorithms to produce sequences of numbers that align with the chosen distribution characteristics.

3.Normal Distribution

The normal distribution, also known as the distribution is a concept, in statistics. It is characterized by a bell shaped curve, where the majority of data points cluster around the mean) value. In the given example generating numbers from a distribution involves creating a set of values that are likely to be spread symmetrically around a specific mean value. The extent of the datas spread is determined by the deviation with values resulting in a wider curve.



4.Uniform Distribution

The uniform distribution is a probability distribution where all values, within a specified range have a chance of occurring. When it comes to the given example generating numbers, from a distribution means producing values that are equally likely to be anywhere within a specified range. To put it simply it’s akin, to selecting numbers from a hat, where each number has a chance of being picked.

To generate numbers from distributions in R we can follow these general steps:

Example 1: Generating Random Numbers from a Normal Distribution

Now lets understand it with the help of an example that will demonstrate how we can generate random number from the normal distribution:

In this illustration we begin by establishing a starting point (using set.seed()) to guarantee that the random numbers produced can be replicated in the future. Then we utilize the rnorm() function to generate 1000 numbers from a distribution. The mean parameter specifies the value of the distribution while the sd parameter determines its deviation. The resulting random numbers are stored in the variable called random_numbers. Lastly we employ print(head(random_numbers)) to showcase the generated random numbers.




# Set the seed for reproducibility
set.seed(123)
 
# Generate 1000 random numbers from a normal distribution
mean_value <- 5
sd_value <- 2
num_samples <- 1000
random_numbers <- rnorm(num_samples, mean = mean_value, sd = sd_value)
 
# Display the first few generated random numbers
print(head(random_numbers))

Output:

[1] 3.879049 4.539645 8.117417 5.141017 5.258575 8.430130

Example 2: Generating Random Numbers from a Uniform Distribution

Now lets understand it with the help of an example that will demonstrate how we can generate random number from the uniform distribution:

In this scenario again we establish a starting point, for reproducibility using set.seed(). Following that we utilize the runif() function to generate 1000 numbers from a distribution. The min parameter determines the value within this distribution while max sets its value. The produced random numbers are stored in a variable named random_numbers. Finally print(head(random_numbers)) is used to exhibit the generated random numbers.




# Set the seed for reproducibility
set.seed(456)
 
# Generate 1000 random numbers from a uniform distribution
min_value <- 0
max_value <- 10
num_samples <- 1000
random_numbers <- runif(num_samples, min = min_value, max = max_value)
 
# Display the first few generated random numbers
print(head(random_numbers))

Output:

[1] 0.895516 2.105123 7.329553 8.521335 7.883979 3.319600

Example 3: Generating Random Numbers from Normal and Uniform Distribution

In this example, we will first generate normal number that is going to store 10 random numbers from the standard normal distribution and another uniform number that will store 20 random numbers from a uniform distribution between 0 and 1.




# Generate 10 random numbers from the standard normal distribution
normal_numbers <- rnorm(10)
 
# Generate 20 random numbers from a uniform distribution between 0 and 1
uniform_numbers <- runif(20, min = 0, max = 1)
 
#Now printing the results
print(normal_numbers)
print(uniform_numbers)

Output:

 print(normal_numbers)
[1] 0.1452898 -0.3818186 -0.5622508 1.2693677 -1.2185140 -1.1886785 -0.7898473
[8] 0.2179803 -1.8060920 0.6974273

print(uniform_numbers)
[1] 0.489666144 0.971377420 0.154619006 0.551200452 0.291894741 0.873487155
[7] 0.311265364 0.899238142 0.962474606 0.302439889 0.878939142 0.611763704
[13] 0.154763431 0.731105502 0.002011918 0.612236707 0.635634828 0.865901164
[19] 0.085477240 0.585402646

Article Tags :