The randint method of the random module
The randint
method of the random
module returns a pseudo-random integer from a given range. In the first parameter of the method, we specify the initial value of the range, in the second - its final value.
Syntax
import random
random.randint(beginning, end)
Example
Let's generate a number in the range from 0
to 20
:
print(random.randint(0, 20))
Result of code execution:
19
Example
Now let's generate a number in the range of negative numbers:
print(random.randint(-15, -5))
Result of code execution:
-6
See also
-
method
random
of modulerandom
,
which returns a pseudo-random number -
method
seed
of modulerandom
,
which initializes a random number -
method
uniform
of modulerandom
,
which generates a pseudo-random real number from the range -
method
randrange
of modulerandom
,
which returns a random number from a range -
method
sample
of modulerandom
,
which returns a random sample of elements from a sequence