| by Arround The Web | No comments

Python Random Seed

Randomness” is a powerful and useful concept in programming. It can help you create realistic simulations, generate test data, shuffle items, and more. To have more control over the randomness in the code, such as to reproduce the same random sequence for debugging purposes or to ensure that the results are consistent across multiple runs or executions, the “random.seed()” method is used in Python.

This Python post will present you with a complete tutorial on Python’s “random.seed()” method via the below content:

What is the “random.seed()” Method in Python?

In Python, the “random.seed()” method initializes the pseudo-random number/num generator (PRNG). The pseudo-random num/number generator is an algorithm that generates numbers that approximate randomness. These random numbers can be reproduced/regenerated utilizing the seed value. A PRNG will start utilizing a seed value from arbitrary starting states if you provide a seed value.

Syntax

random.seed(a, version)

 
In the above syntax:

    • The “a” parameter specifies any seed value used to produce a random number. By default, it utilizes the current/present system time.
    • The parameter named “version” is an int specifying how to convert the “a” parameter into an integer. The default value is “2”.

The “random.seed()” method returns nothing.

Working of the “random.seed()” Method

To get/find the identical sequence of random numbers upon each code execution, the “random.seed()” method is used in Python. The “seed()” method sets the initial state of the random function based on the number you provide. This number is called the seed value and can be any integer.

If you don’t provide a seed value, the random function will use the current system time as the seed value. This means that you will get different random numbers every time you run your code unless you manually set the same seed value.

Example 1: Applying the Same Seed Value Two Times

The following code uses the same seed value twice:

import random
random.seed(44)
print(random.randint(0, 55))

random.seed(44)
print(random.randint(0, 55))

 
In the above code, the “random.seed()” method takes the same specified seed value “44” twice and generates the same random number between the range of “0” to “55” in both cases using the “random.randint()” method.

Output


The same random number has been generated using the identical seed value.

Note: If the random seed value does not match, a different random number is generated after every execution for both cases shown in the above example.

 

Example 2: Using the Specified Seed Value

The following code generates random numbers based on the specified seed value:

import random
random.seed(44)
output = random.randint(0, 55)
print(output)

 
In the above code lines:

    • The “random” module is imported.
    • The “random.seed()” method takes the integer “44” as an argument that indicates the seed value.
    • Lastly, the “random.randint()” method generates the corresponding random number.

Output


The random number has been generated, and the seed value is assigned successfully.

Conclusion

The “random.seed()” method reproduces the random function results repeatedly, as this function initializes the Python pseudo-random num/number generator. The same seed value gets the same random number whenever the random function is executed. This Python write-up presented a thorough guide on the “random.seed()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply