25335/what-exactly-is-the-function-of-random-seed-in-python

I am a bit confused on what random.seed() does in Python. For example, why does the below trials do what they do (consistently)?

>>> import random >>> random.seed(9001) >>> random.randint(1, 10) 1 >>> random.randint(1, 10) 3 >>> random.randint(1, 10) 6 >>> random.randint(1, 10) 6 >>> random.randint(1, 10) 7

I couldn't find good documentation on this. Thanks in advance!

Pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value. Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice. Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.

Hope it works!!

If you are a beginner and need to know more about Python, It's recommended to go for Python Certification course today.

Thanks!

The seed method is used to initialize the pseudorandom number generator in Python.

The random module uses the seed value as a base to generate a random number. if seed value is not present it takes system current time. if you provide same seed value before generating random data it will produce the same data.

Example:

import random random.seed( 30 ) print ("first number - ", random.randint(25,50)) random.seed( 30 ) print ("Second number- ", random.randint(25,50)) Output: first number - 42 Second number - 42

Hey @Rahul.. I get the same output when I run your code from a file but when I run the same code in the python console, I get different output. Can you please tell me why is that so?

@kumar, You need to run random.seed(30) again to set the seed back to its previous value. Generally, the seed is the previous value generated by the generator. So, when you ran random.randint(25,50) second time, your seed was 42 and not 30. If you run random.seed(30) again, 42, 50,...will be the sequence of numbers generated.

import random for i in range(2): random.seed(10) for i in range(20): print(random.random()) print()

Use the above code to see a repeated output for yourself

The seed() is one of the methods in Python's random module. It initializes the pseudorandom number generator. You should call it before generating the random number. If you use the same seed to initialize, then the random output will remain the same.

Code

import random as r r.seed( 13 ) print("First iteration - ", r.randint(2, 40)) r.seed( 13 ) print("Second iteration - ", r.randint(2, 40))

Output

First iteration - 18 Second iteration - 18

How Does Random Work? Nearly all of the functions in this module depend on the basic random() function, which will generate a random float greater than or equal to zero and less than one. Python uses the Mersenne Twister to generate the floats. It produces 53-bit precision floats with a period of 2**19937-1.

The rand() function is used in C/C++ to generate random numbers in the range [0, RAND_MAX). Note: If random numbers are generated with rand() without first calling srand(), your program will create the same sequence of numbers each time it runs.

Definition and Usage

The seed() method is used to initialize the random number generator.

The random number generator needs a number to start with (a seed value), to be able to generate a random number.

By default the random number generator uses the current system time.

Use the seed() method to customize the start number of the random number generator.

Thanks, @cyberradarsystems for your contribution

Register/Sign up on the community to gain points for further contributions. You may ask questions, answer, upvote, and downvote an answer. Each of these would fetch you points and you could be among the top contributors and win exciting merchandise from Edureka. .

raw_input fuction is no longer available in ...READ MORE

The hash() method returns the hash value of an object if it ...READ MORE

Polymorphism is the ability to present the ...READ MORE

Easiest way: math.factorial(x) (available in 2.6 and ...READ MORE

Assumming a is a string. The Slice ...READ MORE

You can try the following in a ...READ MORE

You can also use the random library's ...READ MORE

Syntax : list. count(value) Code: colors = ['red', 'green', ...READ MORE

Enumerate() method adds a counter to an ...READ MORE

You can simply the built-in function in ...READ MORE

OR

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

Already have an account? Sign in.