Home  >  Article  >  Backend Development  >  How to use the seed() method in Python

How to use the seed() method in Python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-17 09:27:495948browse

How to use the seed() method in Python

How to use the seed() method in Python? The following is a relevant introduction to the seed() method:

Description

The seed() method changes the seed of the random number generator. This function can be called before calling other random module functions.

Syntax

The following is the syntax of the seed() method:

import random
random.seed ( [x] )

Note: seed() cannot be accessed directly and needs to import the random module , and then call this method through the random static object.

Related recommendations: "Python Video Tutorial"

Parameters

x -- Change the seed of the random number generator . If you don't understand the principle, you don't have to set the seed specifically, Python will choose the seed for you.

Return value

This function has no return value.

Example

The following shows an example of using the seed() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*- 
import random
random.seed( 10 )
print "Random number with seed 10 : ", random.random()
# 生成同一个随机数
random.seed( 10 )
print "Random number with seed 10 : ", random.random()
# 生成同一个随机数
random.seed( 10 )
print "Random number with seed 10 : ", random.random()

The output result after running the above example is:

Random number with seed 10 :  0.57140259469
Random number with seed 10 :  0.57140259469
Random number with seed 10 :  0.57140259469

The above is the detailed content of How to use the seed() method in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn