Python3 seed() Function
Description
seed() The method changes the seed of the random number generator; call it before calling other random module functions.
Syntax
The syntax of the seed() method is as follows:
import random
random.seed ( [x] )
When we call random.random() to generate a random number, each number generated is random. However, if we set the seed in advance with random.seed(x), where x can be any number such as 10, then calling it first makes the random numbers generated with random() the same each time.
**Note:** seed() cannot be accessed directly; you need to import the random module and call the method through the static random object.
Parameters
- x -- changes the seed of the random number generator. If you do not understand how it works, you do not need to set the seed specially; Python will choose one for you.
Return Value
This function has no return value.
Example
The following shows examples of using the seed(() method:
Example
#!/usr/bin/python3
import
random
random
.
seed
(
)
(
"Random number generated with the default seed:"
,
random
.
random
(
)
)
(
"Random number generated with the default seed:"
,
random
.
random
(
)
)
random
.
seed
(
10
)
(
"Random number generated with integer seed 10:"
,
random
.
random
(
)
)
random
.
seed
(
10
)
(
"Random number generated with integer seed 10:"
,
random
.
random
(
)
)
random
.
seed
(
"hello"
,
2
)
(
"Random number generated with a string seed:"
,
random
.
random
(
)
)
Running the example above produces:
使用默认种子生成随机数: 0.7908102856355441
使用默认种子生成随机数: 0.81038961519195
使用整数 10 种子生成随机数: 0.5714025946899135
使用整数 10 种子生成随机数: 0.5714025946899135
使用字符串种子生成随机数: 0.3537754404730722
Python3 Numbers