Python3 seed() Function

Python3 数字Python3 Numbers


Description

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


Syntax

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

import random

random.seed ( [x] )

When we call random.random() to generate random numbers, the numbers generated each time are random. However, when we preset the seed using random.seed(x), where x can be any number, such as 10, in this case, if we call it first, the random numbers generated using random() will be the same.

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


Parameters

  • x -- Changes the seed of the random number generator. If you don't understand the principle, you don't need 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:

Example

#!/usr/bin/python3
import random

random.seed()
print ("Generate random numbers using the default seed:", random.random())
print ("Generate random numbers using the default seed:", random.random())

random.seed(10)
print ("Generate random numbers using the integer 10 as the seed:", random.random())
random.seed(10)
print ("Generate random numbers using the integer 10 as the seed:", random.random())

random.seed("hello",2)
print ("Generate random numbers using a string as the seed:", random.random())

After running the above example, the output result is:

使用默认种子生成随机数: 0.7908102856355441
使用默认种子生成随机数: 0.81038961519195
使用整数 10 种子生成随机数: 0.5714025946899135
使用整数 10 种子生成随机数: 0.5714025946899135
使用字符串种子生成随机数: 0.3537754404730722

Python3 数字Python3 Numbers

Other Extensions