Chapter #7 – Functions

Chapter #7 – Q #3
Out of random() and randint(), which function should we use to generate random numbers between 1 and 5. Justify.
To generate random numbers between 1 and 5 in Python, you should use the randint() function.
Inclusive Range: The randint(start, end) function generates a random integer within the specified range, including both endpoints. Therefore, calling randint(1, 5) will yield a random integer that can be 1, 2, 3, 4, or 5
Functionality of random(): On the other hand, the random() function generates a random floating-point number between 0.0 and 1.0, which is not suitable for generating integers within a specific range. To use random() to get integers, you would have to scale and convert the result, which complicates the process unnecessarily
In summary, for generating random integers in a specific range like 1 to 5, randint() is the appropriate choice due to its simplicity and directness in providing inclusive integer results.
NCERT Computer Science Class 11 Chapter 7 Solutions Q 3