Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (55.6k points)

Can anyone explain the random library in Python?

1 Answer

0 votes
by (119k points)

In Python, the random library has functions that are used to generate random numbers. Here are a few examples using the random library:

import random

random.seed(1) #To get the same set of random numbers, use same seed value

print(random.random())

print(random.randint(3, 9)) #To get the random number between 3 and 9 (includes both)

mylist = ["abc", "def", "ghi"]
print(random.choice(mylist)) #To select the random element from the sequence

print(random.uniform(5,10)) #To return a float between two numbers (includes both)

You can register for this Python Certification course by Intellipaat to learn Python from top experts:

Related questions

+8 votes
2 answers
0 votes
1 answer
asked Mar 1, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
asked May 15, 2020 in Python by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked May 15, 2020 in Python by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Sep 17, 2019 in Python by Sammy (47.6k points)

Browse Categories

...