Random Number generation in python

In this example, We will write program that generate random number.

For generate random number either we can write complex logic or we can simply use python random module.

Example :

    
# importing the random module
import random

x = random.randint(0,100)
print(x);
    

Output :

    
//Output 1:
68
//Output 2:
77
    

Explanation :

In above example, random.randint() function will generate random number from given start value and end value. Here 1 is start value and 100 is end value so number generated by this program will be between 1 to 100.

We can also chose random number from given array like below example.

Example - 2 :

    
# importing the random module
import random

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

x = random.choice(numberList)
print(x);
    

Output :

    
//Output 1:
2
//Output 2:
5
    

In this example, it will return random number from given values.


Share your thoughts

Ask anything about this examples