import random


def wait_for_small_enough_number(small_number=10, max_number=50, print_it=True):
    """
    What comes in:  Two optional positive integers,
      with the second greater than the first,
      and an optional indicator for whether to print intermediate results.
    What goes out:
      Returns the number of random integers that are generated,
      as described below.
    Side effects:
      -- Repeatedly generates random integers between 1 and max_number,
           inclusive, where max_number is the second given integer.
      -- Stops when the random integer is less than or equal to
           small_number, where small_number is the first given integer.
      -- Optionally prints the random numbers as they are generated.

    Note that, each time through the loop, the probability that the
    stopping-event occurs is   small_number / max_number  (0.2 by default).
    Probability theory tells us that the expected number of times that
    the loop will run is max_number / small_number  (5 by default).
    """
    if print_it:
        print()
        print("----------------------------------------------------------")
        print("Demonstrating WAIT FOR A SMALL ENOUGH")
        print("  randomly generated number")
        print("----------------------------------------------------------")

        print("I will now generate random integers between")
        print("1 and {}, stopping when a generated random".format(max_number))
        print("integer is less than or equal to {}.".format(small_number))
        print()

    count = 0
    while True:
        count = count + 1
        number = random.randrange(1, max_number + 1)
        if print_it:
            print("   Randomly generated number: {}".format(number))
        if number <= small_number:
            break

    if print_it:
        print("{} random integers between 1 and {} were generated".format(
            count, max_number))
        print()
    
    return count