NCERT Computer Science Class #11 – Chapter #7 Solutions – Q #3

Chapter #7 – Functions

NCERT Computer Science Class #11 – Chapter #7 Solutions – Q #3

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

Posts created 29

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top