Python Random Number Generator

Write a Python program to generate a random number (float) between 0 and n or a program for the random integer number generator in a range. To work with the following functions, we have to import a random module.

Remember, the outputs shown below may be different from what you get. Because these Python number functions generate random numbers every time, you call.

Python random number generator between 0 and 1

The random() function generates a number between 0 and 1, and the data type will float. So the below Python number generator example returns random floating point numbers from 0 to 1.

import random as rd

rnum = rd.random()

print(rnum)
0.9625965525945374

Python random integer number in a range using randint

The randint() function takes two arguments. The first argument is the start value, and the second argument is the stop value. Here, the start is 0, and the stop is n.

If you want to generate random numbers between a start value and some other value that is not a stop, then use the below code.  For instance, the below code returns a number between 10 and 100.

rnum = rd.randint(10, 100)

print(rnum)
70

Python random integer number in a range using randrange

The randrange() function is really helpful when you need to pick or generate random numbers from a range of integers. If you pass in a step value, it will only pick a value from the set of integers by skipping over that many numbers. In the last statement, we pass in an integer after the step val.

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

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

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

For loop Examples

If we use the above functions with the combination of for loop, it is beneficial to test or simulate with fake data. By default, the range function skips one number at each step. Thus, we can see that the range doesn’t require a step argument. If you want to skip others.

Python random number generator using for loop & randint

Because this tells the for loop to generate an integer for each iteration. The below generates the numbers between 100 and 200.

We also added an extra print statement to print the numbers generated at each for loop iteration. By this Python random integer number generator in a range example, you can understand it better.

rndList = []

for i in range(1, 11):
    rnum = rd.randint(10, 100)
    rndList.append(rnum)
    print(rnum)

print(rndList)
16
23
72
51
63
78
39
47
80
46
[16, 23, 72, 51, 63, 78, 39, 47, 80, 46]

Python random number generator using for loop & randrange

In the following two examples, we haven’t used the import package line. In order to test the second and third examples, add the import line from the above.

Within the second example, we used the Python randrange function along with for loop.

rndList = []

for i in range(0, 8):
    rnum = rd.randrange(5, 95)
    rndList.append(rnum)
    print(rnum)

print(rndList)
70
62
58
53
44
60
79
73
[70, 62, 58, 53, 44, 60, 79, 73]

Generate random float-point numbers using randint

In this third Python example, we used it within the for loop to print ten random floating point numbers between 10 and 100.

rndList = []

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

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

Python random numbers between 1 and 10 using sample()

The random module includes a sample() that allows you to select one or more elements from a list or a tuple. You can use the choice function to select one element from a sequence.  To use this, pass in a sequence, along with a sample size (how many elements to sample).

It will return a section of elements from the sequence. For example, the below program returns eight numbers between 1 and 10.

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

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

Python random integer number in a range(n to n)

Here, we allow users to enter the start and stop values and generate the number between those values using different functions. Here, the code might be slightly different from the image because we removed the extra text in it to keep the code simple.

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

rnum1 = rd.randint(s, e)
print("using randint   = ", rnum1)

rnum2 = rd.randrange(s, e)
print("using randrange = ", rnum2)

rndList = rd.sample(range(s, e), 7)
print("List using sample    = ", rndList)

rndList1 = []
rndList2 = []

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

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