How do you randomize a seed in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    random[] function is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random[] function generates numbers for some values. This value is also called seed value. 

    Syntax : random.seed[ l, version ]

    Parameter : 

    • l : Any seed value used to produce a random number.
    • version : A integer used to specify how to convert l in a integer.

    Returns: A random value.

    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. 

    Using random.seed[] function

    Here we will see how we can generate the same random number every time with the same seed value. 

    Example 1: 

    Python3

    import random

    for i in range[5]:

        random.seed[0]

        print[random.randint[1, 1000]] 

    Output:

    865
    865
    865
    865
    865

      Example 2: 

    Python3

    import random

    random.seed[3]

    print[random.randint[1, 1000]]

    random.seed[3]

    print[random.randint[1, 1000]]

    print[random.randint[1, 1000]]

    On executing the above code, the above two print statements will generate a response 244 but the third print statement gives an unpredictable response. 

    Uses of random.seed[]

    • 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.
    • 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.

    random.seed[a, version] in python is used to initialize the pseudo-random number generator [PRNG].

    PRNG is algorithm that generates sequence of numbers approximating the properties of random numbers. These random numbers can be reproduced using the seed value. So, if you provide seed value, PRNG starts from an arbitrary starting state using a seed.

    Argument a is the seed value. If the a value is None, then by default, current system time is used.

    and version is An integer specifying how to convert the a parameter into a integer. Default value is 2.

    import random
    random.seed[9001]
    random.randint[1, 10] #this gives output of 1
    # 1
    

    If you want the same random number to be reproduced then provide the same seed again

    random.seed[9001]
    random.randint[1, 10] # this will give the same output of 1
    # 1
    

    If you don't provide the seed, then it generate different number and not 1 as before

    random.randint[1, 10] # this gives 7 without providing seed
    # 7
    

    If you provide different seed than before, then it will give you a different random number

    random.seed[9002]
    random.randint[1, 10] # this gives you 5 not 1
    # 5
    

    So, in summary, if you want the same random number to be reproduced, provide the seed. Specifically, the same seed.

    How do you randomize in Python?

    Random integer values can be generated with the randint[] function. This function takes two arguments: the start and the end of the range for the generated integer values. Random integers are generated within and including the start and end of range values, specifically in the interval [start, end].

    How do you calculate random seed?

    For example, “take a number x, add 900 +x, then subtract 52.” In order for the process to start, you have to specify a starting number, x [the seed]. Let's take the starting number 77: Add 900 + 77 = 977. Subtract 52 = 925.

    What is random seed in Numpy?

    The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.

    What is seed in random sampling?

    As an example, many say that the seed is a starting point of random number generator and the same seed always produces the same random number.

    Chủ Đề