.seed()
In the Python random
module, the .seed()
method is used to create a pseudo-random number generator. Pseudo-random number generators appear to produce random numbers by performing some operation on a value. This value is the seed and it sets the first “random” value of the number sequence. With seeds, a user is able to reproduce the same pseudo-random numbers multiple times.
Syntax
The .seed()
method sets the first random number of the generator, either with a value
or without one:
random.seed(value)
random.seed()
The value
can be an int
, float
, str
, byte
, bytearray
, or NoneType
. If one is not provided, the random number generator will use the current system time to create the seed.
Example
In most cases, the .seed()
method uses the current time of the computer’s system to initialize a new generator:
import randomrandom.seed()print(random.random())
Codebyte Example
In the example below, the .seed()
method is used three times, once with no value and the other two with the same value of 5
. In the output, each call to random.seed(5)
guarantees the first pseudo-random value from the output will be the same every time:
All contributors
- Anonymous contributorAnonymous contributor186 total contributions
- THE-Spellchecker154 total contributions
- BrandonDusch580 total contributions
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- Anonymous contributor
- THE-Spellchecker
- BrandonDusch
- christian.dinh
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.