.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
- THE-Spellchecker
- BrandonDusch
- christian.dinh
- Anonymous contributor
- Anonymous contributor
Contribute to Docs
- 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.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours