Print two different numbers within a range

Use an integer seed. The input bounds must differ so at least two integer values are available.

Write a function named two_random_numbers that takes two integer parameters, min_range and max_range, plus an optional random seed. It must repeatedly generate random integers with a while loop until it has two different numbers, then return the two numbers as a tuple. The bounds are inclusive. If the bounds are supplied in reverse order, swap them. You may assume the range contains at least two integers.

TipRandom Numbers Between Two Integers

Create a local generator with rng = Random(seed), then use rng.randint(low, high) to generate a random integer from low to high, inclusive.

Start from the code in Listing 1.

Listing 1: Starter code and tests for two_random_numbers (Question 1).
from random import Random


def two_random_numbers(
    min_range: int, max_range: int, seed: int = 400
) -> tuple[int, int]:
    # Add code here!
    pass


# Your answer has to produce two different numbers within each range.
for bounds in [(1, 10), (50, 100), (20, 10)]:
    first, second = two_random_numbers(*bounds)
    low, high = min(bounds), max(bounds)
    assert low <= first <= high
    assert low <= second <= high
    assert first != second
    print(first, second)
  • assert condition checks a Boolean condition. A true condition produces no output; a false condition raises AssertionError. For example, assert 2 + 3 == 5 passes.
  • Random(seed) creates a local generator. rng.randint(low, high) returns one integer from the inclusive bounds, which must satisfy low <= high. For example, rng.randint(5, 5) returns 5.
  • min(values) and max(values) return the smallest and largest supplied values. For example, min((20, 10)) is 10, and max((20, 10)) is 20. *bounds passes the tuple items as separate arguments.
  • print(value) writes the value and a newline to the terminal. With several arguments, print(first, second) separates them with a space; for example, print(3, 5) displays 3 5. It returns None.