Python Random is a built-in module that can be used to generate or manipulate random numbers. These types of functions are used in a lot of applications like lotteries, games, or any application that requires a random number generation. Python provides such an important toolkit.
Importance of Random Number Generators
The usefulness of Randomness and generating random numbers should be an obvious one for even a non-programming person. Even in everyday life, dice are used to generate an unpredictable number to define a player’s next move. Also, it is clear that playing cards are well shuffled before beginning the game. So, random numbers bring unpredictability and uniqueness which in turn enables security. Random numbers have high importance in computing. DNS port numbers, TCP/IP sequence numbers, passwords, etc. relies on random numbers.
Types of Random Number Generators
Since computers as such do not have inbuilt randomness, it is not possible to generate random numbers without additional software. A good random number generator has two parts:
- Entropy, a hardware Random number generator (RNG) uses non-deterministic inputs from the surrounding environment in the physical form of measurements like temperature, clock signals, wind speed, atmospheric pressure, etc.
- Cryptographic algorithm is a Pseudo-random number generator (PRNG) producing artificial random bits using keyboard presses and mouse movements and generates random numbers.
Python Random Number Generator
Python offers an easy to use a module called Random to handle random numbers. The Random module has several methods that have the ability to generate random numbers and can solve many programming challenges around randomness. Hence the Python Random module is called Random Number Generator. This module depends on the Pseudo random number generator (PRNG) function random().
Examples of Python Random
Since all the Python random number generator functions are based on random module, initially we need to import random using ‘import random’ command. Below are some examples:
>>> import random >>> random.random() ## this is a random number between 0 and 1 >>> 0.06356280666474168
This is the most important primary function in the Random module and several other functions depend on it. This function generates a random float value between 0.0 and 1.0. The function doesn’t need any arguments.
This function takes specific integers arguments and generates random integer value between the specified integers provided. We can call it using random.randit() method.
See an example below:
>>> import random >>> random.randint(1,5) # a random number between 1 and 5 >>> 3
This function influences all the other functions of the Python Random module that we use after calling it. If we don’t set seed for our pseudo-random number generation, the default value is our current system time. So, we can set this manually using random.seed() method.
- Other examples:
There are many other functions in Python Random module that can be used to make random numbers like uniform(), random.randrange(), random.choice(), random.shuffle(), and a lot more.
Good random number generation is not an easy task due to dependencies such as cryptography. But if a concrete pseudo-random number is enough for our application, Python offers this super simple toolkit of Python Random Number Generating module, ‘Python Random’ which always facilitates a number of easy ways to quickly achieve our goal.
Last but not least, in case you want to learn more about random number generators, please check the Youtube video below: