To generate random numbers in Python, you can use the random
module.
Here’s an example of how you can generate a random floating point number between 0 and 1:
1 2 3 4 5 6 | import random x = random.random() print(x) |
To generate a random integer between a range of values, you can use the randint
function.
Here’s an example of how you can generate a random integer between 1 and 10:
1 2 3 4 5 6 | import random x = random.randint(1, 10) print(x) |
To generate a random float between a range of values, you can use the uniform
function.
Here’s an example of how you can generate a random float between 1 and 10:
1 2 3 4 5 6 | import random x = random.uniform(1, 10) print(x) |
To choose a random element from a list, you can use the choice
function.
Here’s an example of how you can choose a random element from a list:
1 2 3 4 5 6 7 | import random items = [1, 2, 3, 4, 5] x = random.choice(items) print(x) |
To shuffle a list randomly, you can use the shuffle
function.
Here’s an example of how you can shuffle a list:
1 2 3 4 5 6 7 | import random items = [1, 2, 3, 4, 5] random.shuffle(items) print(items) |