Python random seed














































Python random seed



Python random  : seed() 


  This article demonstrates how to use random.seed() function to initialize the pseudo-random number generator in Python to get the deterministic random data you want. By setting the custom seed value we can get the determined sequence of random numbers.

How Seed Function Works ?
Seed function is used to save the state of a random function, so that it can generate same random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value). The seed value is the previous value number generated by the generator. For the first time when there is no previous value, it uses current system time.



Syntax :

 random.seed(a , version)


Parameter Value:

ParameterDescription
aOptional. The seed value needed to generate a random number.
If it is an integer it is used directly, if not it has to be converted into an integer.
Default value is None, and if None, the generator uses the current system time.
versionAn integer specifying how to convert the a parameter into a integer.
Default value is 2


Return value :

Return the same random number every time with the same seed value.

Sample Code :

1.
 # random module is imported 
import random 
for i in range(5): 

    # Any number can be used in place of '0'. 
    random.seed(0

    # Generated random number will be between 1 to 1000. 
    print(random.randint(11000))
Output :
865
865
865

2.

 # importing random module 
import random 

random.seed(3

# print a random number between 1 and 1000. 
print(random.randint(11000)) 

# if you want to get the same random number again then, 
random.seed(3
print(random.randint(11000)) 

# If seed function is not used 

# Gives totally unpredictable responses. 
print(random.randint(11000)) 

Output :

244                                                                                                                                           
244                                                                                                                                           
607 


How to get a seed value used by a random generator :

Sometimes it is useful to be able to reproduce the data given by a pseudo-random number generator.  As you already know random data generation is dependent on a seed value. By re-using a seed value, we can regenerate the same data multiple times as multiple threads are not running.

If you are using a custom seed value, you must remember that Python's Random generator doesn't store seed in memory. i.e., It doesn't provide any method to get the current seed value. It is up to you to save the seed if you want to reuse it.

It is not possible to get the automatic seed back out from the generator. But we can try this alternative :-



Uses of random.seed()  :

  1. This is used in the generation of a pseudo-random encryption key. Encryption keys are an important part of computer security. These are the kind of secret keys which used to protect data from unauthorized access over the internet.
  2. It makes optimization of codes easy where random numbers are used for testing. The output of the code sometime depends on input. So the use of random numbers for testing algorithms can be complex. Also seed function is used to generate same random numbers again and again and simplifies algorithm testing process. 

*******END OF ARTICLE*********



More Articles of Vishal Lodhi:

Name Views Likes
Python string zfill 92 0
Python string swapcase 80 0
Python string title 84 0
Python string startswith 89 0
Python string replace 134 0
Python string translate 72 0
Python string rpartition 76 0
Python string partition 66 0
Python string splitlines 95 0
Python string rsplit 65 0
Python string split 65 0
Python string rindex 73 0
Python string rfind 93 0
Python string upper 68 0
Python string lower 67 0
Python string maketrans 75 0
Python string strip 72 0
Python string rstrip 75 0
Python string lstrip 71 0
Python string rjust 81 0
Python string ljust 81 0
Python string len 70 0
Python string join 63 0
Python string casefold 68 0
Python string isprintable 70 0
Python string encode 68 0
Python string isdecimal 73 0
Python string isidentifier 65 0
Python string isupper 62 0
Python string istitle 69 0
Python string isspace 82 0
Python string isnumeric 87 0
Python string isdigit 68 0
Python string islower 72 0
Python string isalpha 82 0
Python string isalnum 78 0
Python string index 78 0
Python string find 82 0
Python string expandtabs() 73 0
Python string endswith 77 0
Python string count 72 0
Python string capitalize 64 0
Python string center 74 0
Python string Introduction 89 0
Python string Template 128 0
Python string Formatter 122 0
Python string printable 169 0
Python string whitespace 124 0
Python string punctuation 84 0
Python string octdigits 75 0
Python string hexdigits 89 0
Python string digits 68 0
Python string ascii_letters 260 0
Python string ascii_lowercase 116 0
Python string ascii_uppercase 121 0
How to Create Download Manager in Python 395 0
Python random weibullvariate 150 0
python random paretovariate 121 0
Python random vonmisesvariate 151 0
Python random normalvariate 158 0
Python random lognormvariate 102 0
Python random gauss 104 0
Python random gammavariate 112 0
Python random expovariate 165 0
Python random betavariate 148 0
Python random triangular 103 0
Python random uniform 129 0
Python random random 108 0
Python random sample 160 0
Python random shuffle 168 0
Python random choices 189 0
Python random choice 151 0
Python random randint 134 0
Python random randrange 182 0
Python random getrandbits 127 0
Python random setstate 121 0
Python random getstate 161 0
Python random seed 159 1
Python random Introduction 159 0

Comments