How to generate fixed random numbers from a given range that yields same output in each execution

Viewed 494
for x in range(16):
    ran.append(random.randint(1, 500))
    # print(ran)

Generating random numbers within a range is easy but how to generate random numbers from a given range that yields the same output in each execution for that particular range in python.

2 Answers

As long as you seed your random with the same seed, you'll get the same series of values:

random.seed(7) # Or any other arbitrary seed

for x in range(16):
    ran.append(random.randint(1, 500))

In random number generators there is usually a seed() function that does that. If the argument to seed is constant, the generated sequence of numbers will be the same. So, just write random.seed(some_number) in the beginning of your code.

Related