English

Out of random() and randint(), which function should we use to generate random numbers between 1 and 5? Justify. - Computer Science (Python)

Advertisements
Advertisements

Question

Out of random() and randint(), which function should we use to generate random numbers between 1 and 5? Justify.

Short/Brief Note
Advertisements

Solution

The 'randint(a,b)' function will return a random integer within the given range as parameters.

'random()' function generates a random floating-point value in the range (0,1)
Therefore, to generate a random number between 1 and 5 we have to use the randint(1,5) function.

Note: 'randint()' is a part of 'random' module so, before using the same in the program, it has to be imported using the statement ‘from random import randint’.

shaalaa.com
  Is there an error in this question or solution?
Chapter 7: Functions - Exercise [Page 170]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 7 Functions
Exercise | Q 3. | Page 170
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×