Skip to content
Exercises · Q3

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

Sikkim CbseNCERTSubjective· 2mImportance★★★★★est
53% · 21/40 Questions
✓ Free question

randint(1, 5) is the right choice — it returns a random integer from 1 to 5 with both ends included, exactly what is asked; random() returns a float in [0.0, 1.0) and cannot produce 1–5 without extra arithmetic.

The idea. The two functions of the random module serve different jobs: random() is the raw generator of a float in the half-open interval 0.0 ≤ x < 1.0, while randint(a, b) is the convenience function for a whole number in the inclusive range a…b. "Random numbers between 1 and 5" (as in a dice-like draw) means the integers 1, 2, 3, 4, 5 — the randint job.

import random

# The right tool:
print(random.randint(1, 5))    # one of 1, 2, 3, 4, 5 — endpoints included

# Why random() alone is unsuitable:
print(random.random())         # a float like 0.5714025946899135, always < 1.0

Sample output (values vary on every run — that is the point of random numbers; this is one possible run)

4
0.5714025946899135

Justification, point by point

random()randint(1, 5)
Returnsfloatint
Range0.0 ≤ x < 1.0 (1.0 excluded)1 to 5, both inclusive
Gives 1–5 directly?neveralways
Argumentsnonelower and upper bound
  • To coax 1–5 out of random() you would need something like int(random.random() * 5) + 1 — extra scaling, conversion, and an easy place for off-by-one mistakes. randint does it correctly in one call.
Watch out

randint is unusual among Python ranges: both endpoints are included (randint(1, 5) can return 5), unlike range(1, 5) which stops at 4. Do not "compensate" by writing randint(1, 6) — that would occasionally produce 6.

✓Final answer

Use random.randint(1, 5): it directly returns a random integer between 1 and 5 inclusive, while random() produces only a float in [0.0, 1.0) and would require error-prone scaling to get whole numbers.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.