How to Create Random Seed Function in Python
If you want a random number of unexpected results for gambling or computer games, you have to create the random number from the built-in method in Python. It is also useful in cryptography because it helps create random numbers that hackers or attackers cannot guess.
The seed
procedure is used to restart the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. This can be any number, but it usually comes from seconds on a computer system’s clock. By default, the random number generator uses the actual time of the computer. Use the method to customize the start number of the random number generator.
Pseudo-random
number generators a task by performing some functions on a value. Generally, this value is the previous number generated by the generator. Anyhow, the first time you use the generator, there is no previous value.
When you call this method, don’t expect it to be a unique number because computers do not always calculate truly random numbers, as it works on some set of pre-defined rules. If you want to add some randomness, you have to create some rules for your process; for example, to create an equation for your process in expression with x, add 600 +x and then subtract it with 30. Let’s take the x (the seed value) as 40.
- Add 600 + 40 = 640
- Subtract 30 = 614
Following the same algorithm, the second random number would be.
- Add 600 + 30 = 630
- Subtract 30 = 600
This simple example follows a certain specified pattern, however, the algorithms behind computer number generation are much more difficult imitating randomness much better than I can do here. But the process still follows a certain pattern, which will be repeated the next time you enter 40, or whatever number you choose into the random seed.
Random Seed Function in Python
In this code block, we will generate a random number between 0 and 100 and generate 3 times for verification purposes. The first and second times, we give the same seed, and the third time, there is no seed or changing the seed value. First, we will import the random functionality in our code and provide the seed as 3, which will give us the value 31 as a random number, then we will provide the same seed, which will provide us the same number again because the seed was same. But when we provide a new seed or no seed, then the value will always be different.
import random
random.seed(4)
print(random.randint(1, 100))
random.seed(4)
print(random.randint(1, 100))
print(random.randint(1, 100))
Output:
31
31
39
Using the random.randint
function would generate a random number and not do anything else. If you wish to maintain your usage of the same randomly generated number, as simple as that, you’re supposed to use the seed function.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn