Simulation in R

Q1) Generate 3000 random numbers from an exponential distribution, hence write a code in R that gives its 75th percentile

The exponential distribution is commonly used to model the time between events in a Poisson process. It is defined by a single parameter rate (Ī») which is the reciprocal of the mean.

# Set seed for reproducibility
set.seed(123)

# Generate 3000 random values from an exponential distribution with rate = 1
exp_data <- rexp(3000, rate = 1)

# Calculate the 75th percentile
percentile_75 <- quantile(exp_data, 0.75)

# Print the result
cat("The 75th percentile of the exponential distribution is:", round(percentile_75, 4), "\n")
## The 75th percentile of the exponential distribution is: 1.4092
hist(exp_data)

Q2) Generate 7000 random numbers from a t distribution, hence write a code in R that gives its 95th percentile.

The t-distribution (Student’s t-distribution) is used instead of the normal distribution when the sample size is small and/or the population standard deviation is unknown. It’s characterized by its degrees of freedom (df) — lower degrees of freedom result in heavier tails

# Set seed for reproducibility
set.seed(456)

# Generate 7000 random numbers from a t-distribution with 10 degrees of freedom
t_data <- rt(7000, df = 10)

# Calculate the 95th percentile
percentile_95 <- quantile(t_data, 0.95)

# Print the result
cat("The 95th percentile of the t-distribution is:", round(percentile_95, 4), "\n")
## The 95th percentile of the t-distribution is: 1.763
hist(t_data)

Q3) Generate 1000 random numbers from a uniform distribution, hence write a code in R that gives its 67th percentile.

A uniform distribution is a type of probability distribution where all values between a specified minimum and maximum are equally likely. In R, the function to generate data from a uniform distribution is:

# Set seed for reproducibility
set.seed(789)

# Generate 1000 random numbers from a uniform distribution between 0 and 1
uniform_data <- runif(1000000, min = 0, max = 1)

# Calculate the 67th percentile
percentile_67 <- quantile(uniform_data, 0.67)

# Print the result
cat("The 67th percentile of the uniform distribution is:", round(percentile_67, 4), "\n")
## The 67th percentile of the uniform distribution is: 0.6697
plot(density(uniform_data))

Q4) Generate 8000 random numbers from a Weibul distribution, hence write a code in R that gives its 75th percentile

The Weibull distribution is a continuous probability distribution used in reliability analysis and survival modeling. It has two parameters: shape (š‘˜) – defines the distribution’s shape scale (Ī») – stretches or shrinks the distribution

# Set seed for reproducibility
set.seed(1010)

# Generate 8000 random numbers from a Weibull distribution
# Using shape = 2, scale = 1 as common values
weibull_data <- rweibull(8000, shape = 2, scale = 1)

# Calculate the 75th percentile
percentile_75 <- quantile(weibull_data, 0.75)

# Print the result
cat("The 75th percentile of the Weibull distribution is:", round(percentile_75, 4), "\n")
## The 75th percentile of the Weibull distribution is: 1.1815
hist(weibull_data)

Q5) Generate 2000 random numbers from a gamma distribution, hence write a code in R that gives its 99th percentile.

The Gamma distribution is a continuous distribution often used to model waiting times or lifetimes of events. It’s defined by: shape (α) – sometimes denoted as k scale (Īø) – also referred to as 1/Ī» (inverse of rate)

# Set seed for reproducibility
set.seed(1111)

# Generate 2000 random numbers from a Gamma distribution
# Using shape = 3 and scale = 2
gamma_data <- rgamma(2000, shape = 3, scale = 2)

# Calculate the 99th percentile
percentile_99 <- quantile(gamma_data, 0.99)

# Print the result
cat("The 99th percentile of the Gamma distribution is:", round(percentile_99, 4), "\n")
## The 99th percentile of the Gamma distribution is: 16.6895
hist(gamma_data)

Q6) Generate 10000 random numbers from a poisson distribution, hence write a code in R that gives its 90th percentile

The Poisson distribution models the number of events occurring within a fixed interval of time or space. It is defined by: Ī» (lambda) = the average number of occurrences (also called the rate)

# Set seed for reproducibility
set.seed(2025)

# Generate 10,000 random numbers from a Poisson distribution with lambda = 4
poisson_data <- rpois(10000, lambda = 4)

# Calculate the 90th percentile
percentile_90 <- quantile(poisson_data, 0.90)

# Print the result
cat("The 90th percentile of the Poisson distribution is:", percentile_90, "\n")
## The 90th percentile of the Poisson distribution is: 7
hist(poisson_data)