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

Python seed() function

Python seed() function

Python Numbers Python 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 example shows the use of the seed() method:

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

random

print

random

.

random

(

)

print

random

.

random

(

)

print

"------- Set the seed -------"

random

.

seed

(

10

)

print

"Random number with seed 10 : "

,

random

.

random

(

)

Generate the same random number

random

.

seed

(

10

)

print

"Random number with seed 10 : "

,

random

.

random

(

)

Generate the same random number

random

.

seed

(

10

)

print

"Random number with seed 10 : "

,

random

.

random

(

)

Running the example above produces:



0.739880443211

0.78969784712

------- Set seed -------

Random number with seed 10 :  0.57140259469

Random number with seed 10 :  0.57140259469

Random number with seed 10 :  0.57140259469

Python Numbers Python Numbers