Program 4: “Modeling uncertainty with the random module” The objective of this problem is two-fold: 1) to learn how to use the web to find python-specific information about functions and methods, and 2) to use the random module to model uncertainty. There are many functions and methods available in the python distribution that are not included in the standard library. One example is the “sqrt()” function included in the math module which returns the square root of its argument. To access functions of a module not included in the standard library, that module must first be imported using an import statement. This statement should be the first statement in the file, above all of the function definitions. In this program you will import and use the random module which enables drawing a random value from a range, container, or a distribution. Go to this page and read about how to use the randrange() and choice() methods of the random module. The links for these methods will take you to pages that describe their usage, syntax including parameters, and give examples of using the method that you can run and experiment with. After you are familiar with these, then use those methods in developing the below program. The program will have two parts, each using a loop and one each for using the randrange() and choice() methods and a loop.
Here's a Python program that meets the requirements you've outlined. The program uses the random
module to generate random numbers and letters based on user input.
import random
def program_header():
print("Welcome to the Random Number and Letter Generator!")
print("This program will help you generate random numbers from a specified range and random letters from a given string.")
def random_number_generator():
print("\n--- Random Number Generator ---")
lower_limit = int(input("Enter the lower limit of the range: "))
upper_limit = int(input("Enter the upper limit of the range: "))
while True:
random_number = random.randrange(lower_limit, upper_limit)
print(f"Your random number {random_number} is pulled from the range {lower_limit} to {upper_limit}.")
continue_prompt = input("Do you want to generate another random number? (yes/no): ").strip().lower()
if continue_prompt != 'yes':
break
def random_letter_generator():
print("\n--- Random Letter Generator ---")
user_string = input("Enter a string to pull random letters from: ")
while True:
random_letter = random.choice(user_string)
print(f"Your random letter '{random_letter}' is pulled from the string '{user_string}'.")
continue_prompt = input("Do you want to generate another random letter? (yes/no): ").strip().lower()
if continue_prompt != 'yes':
break
def main():
program_header()
random_number_generator()
random_letter_generator()
if __name__ == "__main__":
main()
Imports: The program starts by importing the random
module, which is necessary for generating random numbers and letters.
Program Header: The program_header
function prints a welcome message to the user.
Random Number Generator:
random_number_generator
function prompts the user to enter a lower and upper limit for the range.random.randrange(lower_limit, upper_limit)
.Random Letter Generator:
random_letter_generator
function prompts the user to enter a string.random.choice(user_string)
.Main Function: The main
function orchestrates the flow of the program by calling the header and the two generator functions.
Execution: The program is executed by calling the main()
function when the script is run directly.