Open In App

How to Generate Random Numbers from Standard Distributions in R

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Select the distribution function from Rs built in functions (rnorm for normal distribution or runif for uniform distribution).
  • Specify the desired number of numbers you want to generate and provide any parameters specific to the chosen distribution (e.g., mean and standard deviation for normal distribution or minimum and maximum values, for uniform distribution).
  • Save the generated numbers in a variable so that they can be analyzed or visualized on.

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.

R




# 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
  • set.seed(123): This sets the random number generator’s seed to 123. By setting the seed we will get the same sequence of random numbers.
  • mean_value: This line assigns the value 5 to the variable mean_value, which represents the mean of the normal distribution we ‘re going to generate random numbers from.
  • sd_value: This line assigns the value 2 to the variable sd_value, which represents the standard deviation of the normal distribution.
  • num_samples: This line assigns the value 1000 to the variable num_samples, which determines how many random numbers we want to generate.

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.

R




# 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
  • set.seed: This generate the random number generator’s seed to 456.
  • min_value: This line assigns the value 0 to the variable min_value, which represents the lower bound of the uniform distribution.
  • max_value: This line assigns the value 10 to the variable max_value, which represents the upper bound of the uniform distribution.
  • num_samples: This line assigns the value 1000 to the variable num_samples, determining how many random numbers we want to generate.
  • random_numbers: the runif() function is used to generate random numbers from a uniform distribution. The min argument sets the lower bound of the distribution 0 and the max argument sets the upper bound of the distribution 10.

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.

R




# 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
  • normal_numbers: This line generates 10 random numbers from the standard normal distribution using the rnorm() function.
  • uniform_numbers: This line generates 20 random numbers from a uniform distribution between 0 and 1 using the runif() function.
  • print(normal_numbers): This line prints the 10 random numbers generated from the standard normal distribution.
  • print(uniform_numbers): This line prints the 20 random numbers generated from the uniform distribution between 0 and 1.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads