Python 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 don't understand how it works, you don't need to set the seed explicitly; Python will choose it for you.
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
random
.
random
(
)
random
.
random
(
)
"------- Set the seed -------"
random
.
seed
(
10
)
"Random number with seed 10 : "
,
random
.
random
(
)
Generate the same random number
random
.
seed
(
10
)
"Random number with seed 10 : "
,
random
.
random
(
)
Generate the same random number
random
.
seed
(
10
)
"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