Python program that generates a random number between a given range

Created with Sketch.

Python program that generates a random number between a given range

import random

# Generate a random number between 1 and 100
print(random.randint(1, 100))

The program imports the random module and then uses the randint() function to generate a random integer between 1 and 100 (inclusive) and print it.

You can also generate a random number between a given range by using the random() function from the random module, for example:

 
import random

# Generate a random float between 1 and 10
print(random.uniform(1, 10))

This will generate a random float between 1 and 10.

It’s also possible to get a random element from a list or array using the choice() function:

import random

my_list = [1,2,3,4,5,6,7,8,9,10]

# Get a random element from the list
print(random.choice(my_list))

You can use any of the above functions according to your requirement, all of them will give you a random number or element. Please note that the random numbers generated are not truly random, but are pseudo-random, which means they are generated by an algorithm that simulates randomness.

Leave a Reply

Your email address will not be published. Required fields are marked *