Tuesday, January 14, 2025
HomeProgrammingHow to generate a random number in Python?

How to generate a random number in Python?

In Python, you can generate random numbers using the random module. To generate a random integer within a specified range, use random.randint(a, b), where a is the lower bound and b is the upper bound (inclusive). Here’s an example:

See also  Difference between Operating system and Application

import random

random_number = random.randint(1, 10) # Generates a random number between 1 and 10
print(random_number)

For generating a random floating-point number between 0 and 1, use random.random(). Additionally, for more control over random number generation, you can use random.uniform(a, b) for float ranges or random.choice() for random selection from a list.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x