Python Random Number Generator

Write a Python program to generate random numbers between 0 to n using a random module. To work with the following Python random number generator functions, we have to import the random module. Remember, the output shown below may be different from what you get because these functions generate random numbers every time you call.

Python random number between 0 and 1

The random() function in the random module generates a number between 0 and 1, and the data type will be float.

# Python random Number Generator

import random

rnum = random.random()

print(rnum)
0.9625965525945374

Python random integer in a range

The randint() function generates the random integer value between the given range. Here, you can specify the range start and stop value. For instance, the below code prints the random number between 10 and 100.

# Python random Number Generator

import random

rnum = random.randint(10, 100)

print(rnum)
70

The randrange() function also generates the integer value from start to end. The best part is that you can specify the step value to generate a random even number or odd number.

# Python random Number in a range

import random

rnum1 = random.randrange(10)
print(rnum1)

rnum2 = random.randrange(5, 95)
print(rnum2)

rnum3 = random.randrange(10, 200, 2)
print(rnum3)
2
61
186

Generate a List of random numbers using for loop

If we use the above functions with the combination of for loop, we can generate a list of random numbers. Thus, it is beneficial to test or simulate with fake data. First, we use the randint function and for loop to generate ten random numbers between 1 and 100. We also added an extra print statement to print the random number generated at each for loop iteration. By this, you can understand it better.

# Python random Integer Numbers 

import random

rndList = []

for i in range(1, 11):
    rnum = random.randint(10, 100)
    rndList.append(rnum)
    print(i, "Random Number = ", rnum)

print(rndList)
1 Random Number =  16
2 Random Number =  23
3 Random Number =  72
4 Random Number =  51
5 Random Number =  63
6 Random Number =  78
7 Random Number =  39
8 Random Number =  47
9 Random Number =  80
10 Random Number =  46
[16, 23, 72, 51, 63, 78, 39, 47, 80, 46]

In this second example, we used the Python random module randrange function along with for loop.

import random

rndList = []

for i in range(0, 8):
    rnum = random.randrange(5, 95)
    rndList.append(rnum)
    print(i + 1, "Random Number = ", rnum)

print(rndList)
1 Random Number =  70
2 Random Number =  62
3 Random Number =  58
4 Random Number =  53
5 Random Number =  44
6 Random Number =  60
7 Random Number =  79
8 Random Number =  73
[70, 62, 58, 53, 44, 60, 79, 73]

In this third example, we used the random function within the for loop to print five random float numbers between 0 and 1.

import random

rndList = []

for i in range(1, 11):
    rnum = random.randint(10, 100)
    rndList.append(rnum)
    print(i, "Random Number = ", rnum)

print(rndList)
1 Random Number =  46
2 Random Number =  28
3 Random Number =  95
4 Random Number =  53
5 Random Number =  55
6 Random Number =  68
7 Random Number =  70
8 Random Number =  94
9 Random Number =  65
10 Random Number =  95
[46, 28, 95, 53, 55, 68, 70, 94, 65, 95]

Python random number between 1 and 10

The random sample() function returns the list of random values between the given range of specified size. For example, the below program returns eight random numbers between 1 and 10.

# Python random Float Numbers

import random

rndList = random.sample(range(1, 10), 8)

print(rndList)
[2, 4, 1, 5, 7, 8, 6, 3]

Here, we allow users to enter the start and stop values and generates the random number between those values using different functions.

# Python random Number in a range

import random

s = int(input("Please enter the Starting Value = "))
e = int(input("Please enter the Ending Value   = "))

rnum1 = random.randint(s, e)
print("Random Number using randint   = ", rnum1)

rnum2 = random.randrange(s, e)
print("Random Number using randrange = ", rnum2)

rndList = random.sample(range(s, e), 7)
print("List of Random Numbers using sample    = ", rndList)

rndList1 = []
rndList2 = []

for i in range(0, 7):
    rnum3 = random.randint(s, e)
    rndList1.append(rnum3)
    rnum4 = random.randrange(s, e)
    rndList2.append(rnum4)

print("List of Random Numbers using randint   = ", rndList1)
print("List of Random Numbers using randrange = ", rndList2)
Python Random Number Generator 1