If A=random.randint(B, C) assigns a random value between 1 and 6 (both…

2023

If A=random.randint(B, C) assigns a random value between 1 and 6 (both inclusive) to the identifier A, what should be the values of B and C, if all required modules have already been imported ?

  1. A.

    B=0, C=6 / B=0, C=6

  2. B.

    B=0, C=7 / B=0, C=7

  3. C.

    B=1, C=7 / B=1, C=7

  4. D.

    B=1, C=6 / B=1, C=6

Attempted by 1323 students.

Show answer & explanation

Correct answer: D

Answer: Use random.randint(1, 6).

Explanation:

  • random.randint(a, b) returns an integer N such that a <= N <= b (both endpoints included).

  • To get a random integer from 1 to 6 inclusive, set B = 1 and C = 6 and call random.randint(1, 6).

  • Example: import random; A = random.randint(1, 6) # A will be 1, 2, 3, 4, 5, or 6

  • Alternatives (if preferred): random.randrange(1, 7) or random.choice([1,2,3,4,5,6]).

Explore the full course: Rssb Senior Computer Instructor