{"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> Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n x:<\/strong> This is Optional. A seed value is required to generate a random number. version:<\/strong> An integer that specifies how to convert a parameter to an integer. Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you use the same seed value twice, you’ll get the same random number both times.<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" random seed() Method in Python: The random number generator is initialized using the seed() method. To generate a random number, the random number generator requires a starting number (a seed value). The random number generator defaults to using the current system time. To change the random number generator’s starting number, use the seed() method. Note: …<\/p>\n
\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>\nrandom.seed(x, version)<\/pre>\n
\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
\nThe default value is 2.<\/p>\nGiven seed value = 12<\/pre>\n
The random number = 0.4745706786885481<\/pre>\n
Given seed value = 20<\/pre>\n
The random number = 0.9056396761745207<\/pre>\n
Random seed() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number (seed value) as static input and store it in a variable.\r\ngvn_numb = 12\r\n# Pass the given number as an argument to the random.seed() method to generate\r\n# a random number, the random number generator requires a starting number\r\n# (given seed value).\r\nrandom.seed(gvn_numb)\r\n# Print the random number using the random() function after applying the seed()\r\n# method.\r\n# The generator generates a random number based on the seed value, so if the seed\r\n# value is 12, the first random number will always be 0.4745706786885481\r\nprint(\"The random number = \", random.random())\r\n<\/pre>\n
The random number = 0.4745706786885481<\/pre>\n
import random\r\nrandom.seed(12)\r\nprint(random.random())\r\nrandom.seed(12)\r\nprint(random.random())\r\n<\/pre>\n
0.4745706786885481\r\n0.4745706786885481<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number (seed value) as user input using the int(input()) function \r\n# and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the random.seed() method to generate\r\n# a random number, the random number generator requires a starting number\r\n# (given seed value).\r\nrandom.seed(gvn_numb)\r\n# Print the random number using the random() function after applying the seed()\r\n# method.\r\n# The generator generates a random number based on the seed value, so if the seed\r\n# value is 20, the first random number will always be 0.9056396761745207\r\nprint(\"The random number = \", random.random())\r\n<\/pre>\n
Enter some random number = 20\r\nThe random number = 0.9056396761745207<\/pre>\n