Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report