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