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 137 0
Python string swapcase 108 0
Python string title 109 0
Python string startswith 118 0
Python string replace 159 0
Python string translate 99 0
Python string rpartition 96 0
Python string partition 92 0
Python string splitlines 138 0
Python string rsplit 90 0
Python string split 90 0
Python string rindex 99 0
Python string rfind 121 0
Python string upper 95 0
Python string lower 97 0
Python string maketrans 104 0
Python string strip 95 0
Python string rstrip 115 0
Python string lstrip 96 0
Python string rjust 114 0
Python string ljust 113 0
Python string len 97 0
Python string join 92 0
Python string casefold 97 0
Python string isprintable 102 0
Python string encode 95 0
Python string isdecimal 101 0
Python string isidentifier 93 0
Python string isupper 96 0
Python string istitle 94 0
Python string isspace 115 0
Python string isnumeric 118 0
Python string isdigit 99 0
Python string islower 100 0
Python string isalpha 115 0
Python string isalnum 114 0
Python string index 103 0
Python string find 109 0
Python string expandtabs() 91 0
Python string endswith 109 0
Python string count 105 0
Python string capitalize 92 0
Python string center 96 0
Python string Introduction 115 0
Python string Template 160 0
Python string Formatter 169 0
Python string printable 246 0
Python string whitespace 163 0
Python string punctuation 119 0
Python string octdigits 104 0
Python string hexdigits 125 0
Python string digits 91 0
Python string ascii_letters 426 0
Python string ascii_lowercase 204 0
Python string ascii_uppercase 198 0
How to Create Download Manager in Python 772 0
Python random weibullvariate 185 0
python random paretovariate 151 0
Python random vonmisesvariate 198 0
Python random normalvariate 228 0
Python random lognormvariate 134 0
Python random gauss 142 0
Python random gammavariate 158 0
Python random expovariate 232 0
Python random betavariate 235 0
Python random triangular 128 0
Python random uniform 165 0
Python random random 129 0
Python random sample 213 0
Python random shuffle 198 0
Python random choices 278 0
Python random choice 180 0
Python random randint 165 0
Python random randrange 259 0
Python random getrandbits 167 0
Python random setstate 162 0
Python random getstate 208 0
Python random seed 187 1
Python random Introduction 194 0

Comments