Python random.seed() function to initialize the pseudo-random number generator

Posted in /  

Python random.seed() function to initialize the pseudo-random number generator
vinaykhatri

Vinay Khatri
Last updated on February 26, 2024

    In this Python tutorial, you will learn how to use random.seed() function to initialize a pseudo-random number generator to generate the same random data every time. Practically computers do not generate truly random numbers, it uses a seed value as a base or starting point to generate a random number.

    The seed value acts as a pseudo-random number generator, and if the seed value remains the same, the computer will generate the same random number again and again. When we generate a random number using random.randint() the random module, use the computer seed value as a base for generating the random number. The first seed value is determined by using the timestamp of the computer system, then the currently generated random number becomes the seed value for the next random generator's values.

    Python random module provides the seed() function, which allows us to set the seed value for the random number generator. By setting the seed value to a constant number, we can generate the same random number and pick the same random item from a list or range.

    What is a random seed in Python?

    To generate a random number, Python’s random module uses a seed value, which is also known as the random number generator. The seed value is generally determined by the current timestamp(current system time) of the operating system. The seed value act as a starting point or a base value to generate a random number or value. Although the value of the seed is automatically set by the operating system itself, we can also use the random.seed() function to set it to a constant value.

    By setting it to a constant value, we can generate the same random number again and again or pick the same random item from a list again and again. Before we learn how to use the random.seed() function, let’s discuss why and when to use it.

    When and why use the random.seed() in Python?

    The seed value is very important when we want to generate random secure encrypted keys. There, we just not only depend on the automatic seed value suggested by the operating system. We put our own custom seed value to initialize a secure and robust pseudo-random number generator.

    Here comes the random.seed() function. This function allows us to set a custom seed value that generates a pseudo-random number generator which acts as a base value for generating a random number. By setting a custom seed value, we have a predictable source of a random number that is only known to us.

    random.seed() syntax

    import random 
    
    random.seed(a=None, version=2)

    The random seed() function can accept two argument values and both are optional.

    Arguments

    a: It is the seed value. By default its value is None, so it uses the current system time of the operating system. version: It is also an optional argument whose default value is 2. In version 2, the specified seed str, bytes, and byte array objects get converted into an int.

    Generate the same random number every time using random.seed()

    By setting the random.seed() value to a constant number, we can generate the same number every time.

    Example

    import random
    
    #set the random seed to constant number 4
    random.seed(4)
    
    ##generate a random number
    print("random number 1:", random.randint(1,10000))
    
    #set the random seed to constant number 4
    random.seed(4)
    
    ##generate a random number
    print("random number 2:", random.randint(1,10000))

    Output

    random number 1: 3868
    random number 2: 3868

    The seed number is automatically set to a different value after we perform any random generation of an operation. So in order to generate the same random number, we have to set the seed value to a constant before generating the random number every time. If we call the randint() function twice before calling the seed() function, we will not get the same random number.

    Example

    import random
    
    #set the random seed to constant number 4
    random.seed(4)
    
    ##generate a random number
    print("random number 1:", random.randint(1,10000))
    
    ##generate a random number
    print("random number 2:", random.randint(1,10000))

    Output

    random number 1: 3868
    random number 2: 4970

    Here you can see that the random 2 is the completely random number this is because the seed value is set to something different for it.

    Get a seed value used by a random Generator

    Python does not store the seed value in the memory, it is automatically generated with every call of generating some random number. It also does not provide any method or property to get the current seed value. So in order to get the seed value, we have to save our custom seed value in a Python identifier so that we can reuse the seed value throughout the program.

    For instance, if we want to generate the same random result for particular cases in our program, there we can simply call the seed function with custom seed_value before calling any random operation.

    Example

    import random
    
    import sys
    
    # generate a random seed number between 0 to max number
    seed_value = random.randint(0, sys.maxsize)
    
    # print the seed value
    print("Your seed value is:", seed_value)
    
    '''
    now we can use the seed value anywhere
    throughout the program
    when we generate the same result
    '''
    #generate a random number between 100 and 999
    #with the custom seed value
    #set the seed value
    random.seed(seed_value)
    
    print("Random Number 1 (100, 999):",random.randint(100, 999))
    
    random.seed(seed_value)
    
    print("Random Number 2 (100, 999):",random.randint(100, 999))

    Output

    Random Number 1 (100, 999): 842
    Random Number 2 (100, 999): 842

    Common Examples of random.seed() function

    Now you know how to use the Python seed() function, now let’s discuss some of the common use cases of random.seed() function.

    Example 1: Use the random seed to choose the same item from the list randomly.

    With the random choice() function, we can choose a random item from a Python list. The random choice also uses the seed value to pick a random item from the list randomly. And by setting the seed() to a constant value before calling the choice() function, we can pick the same item.

    import random
    
    programming_languages = ['C','C++', 'Java', 'Python', 'JavaScript', 'Ruby', 'Kotlin']
    
    #set the seed value to 50
    random.seed(50)
    print("Random Programming Language 1:", random.choice(programming_languages))
    
    #again set the seed value to 50
    random.seed(50)
    print("Random Programming Language 2:", random.choice(programming_languages))

    Output

    Random Programming Language 1: Python
    Random Programming Language 2: Python

    Example 2: Use the random seed to choose the same multiple items from the list randomly.

    The random sample() function allows us to pick n number of items from the list randomly. Similar to the random.choice() method the sample() method picks the same items if we set a custom seed value before calling the function.

    import random
    
    programming_languages = ['C','C++', 'Java', 'Python', 'JavaScript', 'Ruby', 'Kotlin']
    
    #set the seed value to 50
    random.seed(50)
    print("Random 3 Programming Languages 1:", random.sample(programming_languages,3))
    
    #again set the seed value to 50
    random.seed(50)
    print("Random 3 Programming Languages 2:", random.sample(programming_languages,3))

    Output

    Random 3 Programming Languages 1: ['Python', 'Java', 'Ruby']
    Random 3 Programming Languages 2: ['Python', 'Java', 'Ruby']

    Example 2: Use the random seed to shuffle the list the same randomly.

    The random shuffle() function can randomly shuffle the items of a list. But by setting the seed value before shuffling the list, we can get the same shuffling result.

    import random
    
    programming_languages = ['C','C++', 'Java', 'Python', 'JavaScript', 'Ruby', 'Kotlin']
    
    #set the seed value to 50
    random.seed(50)
    random.shuffle(programming_languages)
    print("Random shuffle 1:",programming_languages )
    
    '''
    redefine the actual order of list
    because the order has been shuffled
    '''
    programming_languages = ['C','C++', 'Java', 'Python', 'JavaScript', 'Ruby', 'Kotlin']
    
    #again set the seed value to 50
    random.seed(50)
    random.shuffle(programming_languages)
    print("Random shuffle 2:",programming_languages )

    Output

    Random shuffle 1: ['C', 'Kotlin', 'JavaScript', 'C++', 'Ruby', 'Java', 'Python']
    Random shuffle 2: ['C', 'Kotlin', 'JavaScript', 'C++', 'Ruby', 'Java', 'Python']

    Conclusion

    To generate a random number, computers use the seed value, which provides a start point or base to generate a random number. To determine the seed value, the random module uses the default operating system time system and uses it as a seed value to generate a random number. The random module also provides the seed() method in case the developer does not want to use the default systems and wants to generate a predictable and secure random number using a custom seed value.

    People are also reading:

    Leave a Comment on this Post

    0 Comments