Random Value Generation - Python Programming Exercise

In this exercise, you will develop a Python project with a class RandomNumber that includes three static methods. This exercise is perfect for practicing class definition, method implementation, and random number generation in Python. By implementing this class, you will gain hands-on experience in handling class definitions, method implementation, and random number generation in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Python Classes in OOP

 Exercise

Random Value Generation

 Objective

Develop a Python project with a class RandomNumber that includes three static methods:

GetFloat: Returns a value between 0 and 1 using the formula:
seed = (seed * a + c) % m
result = abs(seed / m)

GetInt(max): Returns an integer between 0 and max, calculated as:
result = round(max * GetFloat())

GetInt(min, max): Returns an integer between min and max (implement this).

Initialize with:
m = 233280; a = 9301; c = 49297; seed = 1;

Program this in Python and test the methods.

 Example Python Exercise

 Copy Python Code
class RandomNumber:
    # Static variables for random number generation
    m = 233280
    a = 9301
    c = 49297
    seed = 1

    @staticmethod
    def GetFloat():
        """
        Generates a floating point number between 0 and 1 using the formula:
        seed = (seed * a + c) % m
        result = abs(seed / m)
        """
        # Update the seed using the linear congruential generator formula
        RandomNumber.seed = (RandomNumber.seed * RandomNumber.a + RandomNumber.c) % RandomNumber.m
        # Calculate the float value between 0 and 1
        result = abs(RandomNumber.seed / RandomNumber.m)
        return result

    @staticmethod
    def GetInt(max):
        """
        Generates an integer between 0 and 'max' using the formula:
        result = round(max * GetFloat())
        """
        # Generate a float and scale it by the max value
        return round(max * RandomNumber.GetFloat())

    @staticmethod
    def GetIntRange(min, max):
        """
        Generates an integer between 'min' and 'max' using the formula:
        result = round((max - min) * GetFloat()) + min
        """
        # Generate a float value between 0 and (max - min)
        return round((max - min) * RandomNumber.GetFloat()) + min


# Test the RandomNumber class and methods
if __name__ == "__main__":
    print("Testing RandomNumber Class:")

    # Test GetFloat
    print("GetFloat():", RandomNumber.GetFloat())

    # Test GetInt with a maximum value of 10
    print("GetInt(10):", RandomNumber.GetInt(10))

    # Test GetIntRange with a range of 5 to 15
    print("GetIntRange(5, 15):", RandomNumber.GetIntRange(5, 15))

    # Test with different ranges
    print("GetIntRange(100, 200):", RandomNumber.GetIntRange(100, 200))
    print("GetInt(50):", RandomNumber.GetInt(50))

 Output

Testing RandomNumber Class:
GetFloat(): 0.11292986114866488
GetInt(10): 1
GetIntRange(5, 15): 8
GetIntRange(100, 200): 183
GetInt(50): 48

 Share this Python Exercise

 More Python Programming Exercises of Mastering Python Classes in OOP

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Converting Text to HTML

    In this exercise, you will develop and program a Python class "TextToHTML" that can convert multiple texts entered by the user into a sequence of HTML lines. This ...

  •  Text on Screen Class

    In this exercise, you will develop a Python class called "DisplayText" that allows you to show text at specific coordinates on the screen. This exercise is per...

  •  Improved ComplexNumber Class

    In this exercise, you will develop a Python program to enhance the "ComplexNumber" class by overloading the addition (+) and subtraction (-) operators. This exerci...

  •  3D Coordinates

    In this exercise, you will develop a Python class "Point3D" that will represent a point in three-dimensional space with coordinates X, Y, and Z. This exercise ...

  •  Catalog & Navigation

    In this exercise, you will develop a Python program to enhance the Catalog application, where the "Main" function displays a menu that allows users to input new data ...

  •  Object array: tables

    In this exercise, you will develop a Python class called "Table". The class should include a constructor to initialize the width and height of the table. It should al...