Open In App

Monte Carlo Simulation of Bernoulli Trials in R

A Monte Carlo simulation is a statistical method used to generate random sample sets to study and model the behavior of a system. The Bernoulli trial is a type of discrete probability experiment where the outcome can only be two possible results, either success or failure.

Simulating the outcome of flipping a fair coin 10 times. A histogram showing the frequency of the number of heads in 10 coin flips. The distribution should be symmetrical and centered around 5 since each flip has a 50-50 chance of being heads or tails.



  1. Define the probability of success (p).
  2. Define the number of trials (n).
  3. Simulate the Bernoulli trials using rbinom() function in R Programming Language.
  4. Plot the results using hist() or barplot() functions.




# Define probability of success
# and number of trials
p <- 0.5
n <- 10
  
# Simulate the Bernoulli trials
results <- rbinom(1000, size = n, prob = p)
  
# Plot the results
hist(results, main = "Coin Flip Simulation",
     xlab = "Number of Heads", col = "blue")

Output :

 

Simulating the outcome of 10 people taking a test with a 70% passing rate. A histogram showing the frequency of the number of people passing the test in a group of 10. The distribution should be skewed to the right with more values closer to 7 (70% passing rate) and fewer values closer to 0 or 10.






# Define probability of success and number of trials
p <- 0.7
n <- 10
  
# Simulate the Bernoulli trials
results <- rbinom(1000, size = n, prob = p)
  
# Plot the results
hist(results, main = "Test Passing Simulation",
     xlab = "Number of Passes", col = "blue")

Output:

 

Simulating the outcome of 100 patients receiving a new medication with a success rate of 80%. A histogram showing the frequency of the number of successful treatments in 100 patients. The distribution should be skewed to the right with more values closer to 80 (80% success rate) and fewer values closer to 0 or 100.




# Define probability of success and number of trials
p <- 0.8
n <- 100
  
# Simulate the Bernoulli trials
results <- rbinom(1000, size = n, prob = p)
  
# Plot the results
hist(results, main = "Medication Success Simulation",
     xlab = "Number of Successful Treatments",
     col = "blue")

Output :

Monte Carlo Simulations for unbiased coin toss

Simulating the outcome of a biased coin flip with a 60% chance of landing on. A histogram showing the frequency of the number of heads in 10 coin flips. The distribution should be skewed to the right with more values closer to 6 (60% chance of heads) and fewer values closer to 0 or 10.




# Define probability of success and number of trials
p <- 0.6
n <- 10
  
# Simulate the Bernoulli trials
results <- rbinom(1000, size = n, prob = p)
  
# Plot the results
hist(results, main = "Biased Coin Flip Simulation",
     xlab = "Number of Heads",
     col = "blue")

Output :

Monte Carlo Simulations for Biased coin toss


Article Tags :