Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 seed() Function

Python3 seed() Function

Python3 Numbers Python3 Numbers


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


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

(

)

print

(

"Random number generated with the default seed:"

,

random

.

random

(

)

)

print

(

"Random number generated with the default seed:"

,

random

.

random

(

)

)

random

.

seed

(

10

)

print

(

"Random number generated with integer seed 10:"

,

random

.

random

(

)

)

random

.

seed

(

10

)

print

(

"Random number generated with integer seed 10:"

,

random

.

random

(

)

)

random

.

seed

(

"hello"

,

2

)

print

(

"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 Python3 Numbers