The Easiest way to Generate Random Numbers

In this Python example, we will look that how we can generate random numbers using different techniques.

1. What is a Random Number?

random number is a number chosen from a pool of limited or unlimited numbers that has no discernible pattern for prediction.

According to Wikepedia

Some of the topics which will be helpful for understanding the program implementation better are:

2. How to Generate a Random Number using Python?

In Python, we have the random module which contains all the functions to generate random numbers. Mainly, all the methods inside the random module depend on random() which is a pseudo-random number generator.

There are various applications of random number generation, for example, lottery games, pin generation etc.

There are different ways in Python using which we can generate random numbers.

2.1. random.choice function in Python

The choice() is an inbuilt function in a random module that returns a random item from a list, tuple, or string.

import random
# prints a random value from the list
values = [2, 5, 9, 13, 19, 23]

In the above program, the random number will be chosen automatically.

2.2. random.randrange function in Python

The randrange() function generates random numbers from a specified range. It intakes the beginning, the ending, and the stepping value.

#Python Program for Randrange function
import random
print("The number is : ", end="")
print(random.randrange(28, 59, 2))
The number is : 54

2.3. random.random function in Python

As stated before, the random function returns any random float number which is >= 0 and < 1.

import random
print("The numbe rusing random() function: ", end="")
The numbe rusing random() function: 0.9201440577334621

2.4. random.seed function in Python

The seed() function helps to save the current state of a random function for letting the function produce random numbers on multiple executions of the code on the same machine or on different machines.

The seed value uses the value generated by the generator previously. When there is no previous value generated, seed uses the current system time.

import random

print("Random number with seed 6 is : ", random.random())

print("Random number with seed 9 is : ", random.random())
print("Random number with seed 6 is : ", random.random())
Random number with seed 6 is :  0.793340083761663
Random number with seed 9 is :  0.46300735781502145
Random number with seed 6 is :  0.793340083761663

3. Conclusion

In this article, we have discussed different programs for generating random numbers in Python using random, choice, randrange and seed functions.

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x