[Type C] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

4. Write a function that receives two numbers and generates a random number from that range. Using this function, the main program should be able to print three numbers randomly.

Short Answer: A function will be created to generate a random number within the range specified by two given numbers. The main program will use this function to print three random numbers.

Python
import random

def generate_random_num(num1, num2):
    return random.randint(num1, num2)

# Main Program
for _ in range(3):
    print("Random Number:", generate_random_num(1, 10))

Explanation:

  • The function generate_random_num generates a random integer within the range specified by two given numbers using random.randint.
  • The main program invokes this function three times to print three random numbers within the specified range.
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *