📜  random.randint - Python (1)

📅  最后修改于: 2023-12-03 15:34:37.349000             🧑  作者: Mango

random.randint() in Python

random.randint(a, b) is a Python function that returns a randomly selected integer from the range a to b (inclusive).

Here's an example usage:

import random

random_number = random.randint(1, 10)

print(f"The random number is {random_number}")

This will output a random number between 1 and 10, inclusive.

Syntax

The syntax for random.randint() is:

random.randint(a, b)
  • a - the lower bound of the range
  • b - the upper bound of the range
Return Value

random.randint() returns a randomly selected integer between a and b, inclusive.

Example

Here's another example:

import random

magic_number = random.randint(100, 200)
print(f"The magic number is {magic_number}")

This will output a random number between 100 and 200, inclusive.

Conclusion

random.randint() is a useful function when you need a random integer within a specified range. It's easy to use and can be very versatile.