.randint()
Published May 21, 2021Updated May 15, 2024
Contribute to Docs
The .randint()
method returns a random integer that exists between two int
values, int_a
and int_b
(inclusive), passed as arguments.
Syntax
random.randint(int_a, int_b)
Another way of writing this would be random.randrange(int_a, int_b + 1)
.
Example
In the example below, .randint()
is used to return a random number between 0
and 50
:
import randomprint(random.randint(0, 50))
Codebyte Example
The .randint()
method can also be applied to negative int
values, as shown in the example below:
All contributors
- THE-Spellchecker
- BrandonDusch
- Anonymous contributor
- christian.dinh
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours