73.2k views
5 votes
Write a complete program to read a number and then print three random numbers between 1 and that number, inclusive. Make it so that the same 3 numbers will print every time the user enters the same limit.

User Heyitsjhu
by
8.3k points

1 Answer

0 votes

Final answer:

A program that reads a number and prints three random numbers between 1 and that number inclusive can be created by seeding a random number generator. Using the seed ensures that the same three numbers are generated every time the same limit is provided.

Step-by-step explanation:

To read a number and then print three random numbers between 1 and that number, inclusive, and ensure the same 3 numbers print every time the same limit is entered, you can use a seed for the random number generator. Below is a complete program example written in Python:

import random
def generate_same_random_numbers(upper_limit):
random.seed(upper_limit) # Seed the random number generator with the input
numbers = [random.randint(1, upper_limit) for _ in range(3)]
return numbers
# Read the upper limit from the user
upper_limit = int(input("Enter a number: "))
# Generate and print the random numbers
random_numbers = generate_same_random_numbers(upper_limit)
print('The three random numbers are:', random_numbers)
This script takes an upper limit as input, seeds the random number generator with it, and then generates three random numbers between 1 and the input limit.
User Niklas Holsti
by
8.2k points

No related questions found