The random module in Python provides a suite of functions for generating random numbers and sequences. To use the random module, first import it:
import random
Once imported, you can use the various functions available in the module. For example, to generate a random integer between 1 and 10, you can use the randint() function:
random_number = random.randint(1, 10)
To generate a random float between 0 and 1, use the random() function:
random_float = random.random()
To generate a random element from a sequence, such as a list, use the choice() function:
my_list = [1, 2, 3, 4, 5]
random_element = random.choice(my_list)
The random module also provides functions for generating random strings, permutations, and other useful features. For more information, refer to the official Python documentation.
Legutóbbi hozzászólások