{"id":25479,"date":"2021-11-16T08:39:55","date_gmt":"2021-11-16T03:09:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=25479"},"modified":"2021-11-16T08:39:55","modified_gmt":"2021-11-16T03:09:55","slug":"python-random-seed-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-seed-method-with-examples\/","title":{"rendered":"Python Random seed() Method with Examples"},"content":{"rendered":"

random seed() Method in Python:<\/strong><\/p>\n

The random number generator is initialized using the seed() method.<\/p>\n

To generate a random number, the random number generator requires a starting number (a seed value).<\/p>\n

The random number generator defaults to using the current system time.<\/p>\n

To change the random number generator’s starting number, use the seed() method.<\/p>\n

Note:<\/strong> Please keep in mind that if you use the same seed value twice, you will get the same random number both times.<\/p>\n

How Does the Seed Function Work?<\/strong>
\nThe seed function saves the state of a random function so that it can generate the same random numbers on multiple executions of the code on the same or different machines (for a specific seed value). The previous value number generated by the generator serves as the seed value. When there is no previous value, it uses the current system time for the first time.<\/p>\n

Syntax:<\/strong><\/p>\n

random.seed(x, version)<\/pre>\n

Parameters<\/strong><\/p>\n

x:<\/strong> This is Optional. A seed value is required to generate a random number.
\nIf it is an integer, it is used directly; otherwise, it must be converted to an integer.
\nThe default value is None, and if None is specified, the generator will use the current system time.<\/p>\n

version:<\/strong> An integer that specifies how to convert a parameter to an integer.
\nThe default value is 2.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given seed value = 12<\/pre>\n

Output:<\/strong><\/p>\n

The random number =  0.4745706786885481<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given seed value = 20<\/pre>\n

Output:<\/strong><\/p>\n

The random number =  0.9056396761745207<\/pre>\n

Random seed() Method with Examples in Python<\/h2>\n