Home >Backend Development >Python Tutorial >How to Generate Random Integers within a Specific Range in Python?

How to Generate Random Integers within a Specific Range in Python?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-01 05:53:19696browse

How to Generate Random Integers within a Specific Range in Python?

Generate Random Integers within a Specified Range

In Python, generating random integers within a specific range is straightforward. One commonly used method is the randrange() function from the random module. Here's how it can be used to generate random integers between 0 and 9 (inclusive):

from random import randrange

print(randrange(10))

In this code snippet:

  • from random import randrange imports the randrange() function from the random module.
  • print(randrange(10)) generates a random integer between 0 and 9 (inclusive) and prints the result.

The above is the detailed content of How to Generate Random Integers within a Specific Range in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn