pythonarrayspython-3.xlistrandom

How to generate an array so that it does not change?


How to generate a list that will not change until I want it to? random.sample(range(1, 15), 14) every time the program is launched, a new list will be generated, how can I make it so that it does not change until I want it to? So that on another device, maybe I could set some pattern and the same list would appear?

I need the generated list not to change until I want it to, maybe there is some pattern that can be set to random?


Solution

  • Use random.seed (of course, it will only work for random numbers generated with random module and not other random number generators)

    import random
    
    random.seed(0)
    
    print(random.sample(range(1, 15), 14))
    

    This code will generate following numbers: [14, 7, 13, 1, 5, 9, 8, 4, 3, 12, 6, 10, 11, 2] each time it is executed.