Prepin
Log in

Create a function that simulates flipping a coin 20 times, with a 20% chance of landing on heads. After that, execute the function 1,000 times. Finally, generate a histogram to display the results.

Approach / Explanation

This task consists of three main components: Create a function that simulates flipping a coin 20 times, with a 20% chance of landing on heads (since a real coin flip has a 50% chance for heads or tails). Execute this simulation 1,000 times, which means you will repeat the first step a thousand times. Generate a histogram to visualize the distribution of outcomes based on the results from step 2. To achieve these objectives, you should have a solid understanding of: Fundamental programming concepts: You need to write a function, utilize a loop to execute this function repeatedly, and save the outcomes in a suitable data structure. Random number generation: You will likely need to employ random number generation (or events) to mimic the coin flips. Statistics / Probability: To grasp the 20% likelihood of achieving heads and how to model this. Data Visualization: To create the histogram.

Suggested Answer

In Python, you can utilize the numpy library. The random.binomial function is particularly useful for simulating coin tosses. Here’s a code example that demonstrates this process. import numpy as np import matplotlib . pyplot as plt # Function to simulate 20 coin flips with a 20% chance of heads def coin_flip_simulation ( ) : return np . random . binomial ( 20 , 0.2 ) # Execute the function 1000 times and save the results results = [ coin_flip_simulation ( ) for i in range ( 1000 ) ] # Create the histogram plt . hist ( results , bins = range ( min ( results ) , max ( results ) + 1 , 1 ) , align = 'left' ) plt . show ( ) In this script, np.random.binomial(20, 0.2) simulates flipping a coin 20 times and returns the count of successful flips (where "success" is defined as getting heads). A binomial random variable is utilized here since the coin flips are independent and share the same probability of success. This function is executed 1,000 times, and the outcomes are stored in the 'results' list. Finally, a histogram of the results is generated.

Alternative Answer

This issue can be addressed in R by utilizing the rbinom function along with ggplot2 for creating visual representations. library(ggplot2) # Function to simulate 20 coin flips with a 20% probability of heads coin_flip_simulation <- function() { return (rbinom(1, 20, 0.2)) } # Execute the function 1000 times and save the results results <- replicate(1000, coin_flip_simulation()) # Convert the results to a data frame df <- data.frame(results) # Create the histogram ggplot(df, aes(x=results)) + geom_histogram(binwidth=1, color="black", fill="white") The function rbinom(1, 20, 0.2) simulates the outcome of 20 coin flips, where the '1' parameter indicates that you want a single outcome from these 20 flips. The command replicate(1000, coin_flip_simulation()) is employed to execute the function 1,000 times. Lastly, ggplot2 is utilized to generate a histogram displaying the results.

Question Details

Difficulty & Category

Medium
coding

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.