Get Started

The np.random module and Bernoulli trials

You can think of a Bernoulli trial as a flip of a possibly biased coin. Specifically, each coin flip has a probability \(p\) of landing heads (success) and probability \(1-p\) of landing tails (failure). In this exercise, you will write a function to perform n Bernoulli trials, perform_bernoulli_trials(n, p), which returns the number of successes out of n Bernoulli trials, each of which has probability p of success. To perform each Bernoulli trial, use the rng.random() function, which returns a random number between zero and one.

This is a part of the course

“Statistical Thinking in Python (Part 1)”

View Course

Exercise instructions

  • Define a function with signature perform_bernoulli_trials(n, p).
    • Initialize to zero a variable n_success the counter of Trues, which are Bernoulli trial successes.
    • Write a for loop where you perform a Bernoulli trial in each iteration and increment the number of success if the result is True. Perform n iterations by looping over range(n).
      • To perform a Bernoulli trial, choose a random number between zero and one using rng.random(). If the number you chose is less than p, increment n_success (use the += 1 operator to achieve this). An RNG has already been instantiated as the variable rng and seeded.
    • The function returns the number of successes n_success.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def perform_bernoulli_trials(n, p):
    """Perform n Bernoulli trials with success probability p
    and return number of successes."""
    # Initialize number of successes: n_success
    n_success = ____

    # Perform trials
    for i in ____:
        # Choose random number between zero and one: random_number


        # If less than p, it's a success so add one to n_success
        if ____:
            ____

    return n_success

This exercise is part of the course

Statistical Thinking in Python (Part 1)

IntermediateSkill Level
4.6+
30 reviews

Build the foundation you need to think statistically and to speak the language of your data.

Statistical inference rests upon probability. Because we can very rarely say anything meaningful with absolute certainty from data, we use probabilistic language to make quantitative statements about data. In this chapter, you will learn how to think probabilistically about discrete quantities: those that can only take certain values, like integers.

Exercise 1: Probabilistic logic and statistical inferenceExercise 2: What is the goal of statistical inference?Exercise 3: Why do we use the language of probability?Exercise 4: Random number generators and hacker statisticsExercise 5: Generating random numbers using the np.random moduleExercise 6: The np.random module and Bernoulli trials
Exercise 7: How many defaults might we expect?Exercise 8: Will the bank fail?Exercise 9: Probability distributions and stories: The Binomial distributionExercise 10: Sampling out of the Binomial distributionExercise 11: Plotting the Binomial PMFExercise 12: Poisson processes and the Poisson distributionExercise 13: Relationship between Binomial and Poisson distributionsExercise 14: How many no-hitters in a season?Exercise 15: Was 2015 anomalous?

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free