Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the random.seed () method in python

Shulou Source: shulou.com Published: 2022-06-02 07:12:52 10月03日 Update

Python in the random.seed () method how to use, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Description

Initialize the random number generator.

Grammar

Random.seed (a=None, version=2)

Parameters.

A-generates the seed of a random number, which can be set to an integer (int).

Return

There is no return value.

Example sets the random seed # test.pyimport randomrandom.seed (0) print (random.random ()) # to return floating point numbers randomly extracted from the interval [0.0,1.0)

The output is the same each time you run test.py:

0.8444218515250481 No random seed # test.pyimport randomprint (random.random ()) # returns a floating point number randomly selected from the interval [0.0,1.0)

The output is different each time you run test.py:

0.7368183372926044-0.94051675574418-0.5895382014645497 attention

After setting the random seed, the output is the same every time you run the test.py file, not every time the random function generates the same result:

# test.pyimport randomrandom.seed (0) print (random.random ()) print (random.random ())

Output:

0.84442185152504810.7579544029403025

You can see that printing the random.random () function twice produces different results, but if you run test.py again, it will still print:

0.84442185152504810.7579544029403025

However, if you just want the results to be the same every time you run a random function, you can set exactly the same random seed in front of each random function:

# test.pyimport randomrandom.seed (0) print (random.random ()) random.seed (0) print (random.random ())

Output:

0.84442185152504810.8444218515250481 is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Result seed generation output run function interval point random number help extraction method clear same identical content parameter that is integer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple MySQL Shulou Technology Xiaomi