2

I have a job scheduler on an windows instance at aws. The job runs a py script that at some point generates a random number using randint. This is a job running every 7 hours, and after a full cycle it begins to generate the same random numbers that it did frmo the first time it ran.

from random import randint

def randHosp():
    return randint(1, 1442)

This is the function that returns the random number, just a simple call to the randint function.

I know that it generates the random number based on the time, is there a way i can change this ?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
filipe
  • 69
  • 5
  • Helps if you post code! – stacksonstacks Mar 12 '19 at 19:21
  • Hi! just edited the post, i dont know if this is much of an help because its a simple call to the randint function! – filipe Mar 12 '19 at 19:26
  • Not an answer, but [here](https://stackoverflow.com/questions/31356466/is-pythons-random-number-generation-easily-reproducible) is a discussion on the random library, it looks like it may be falling back on the system clock to generate the seed for random number generation. Not sure, but that could be one explanation – G. Anderson Mar 12 '19 at 19:31
  • Which python version are you using? – stacksonstacks Mar 12 '19 at 19:50
  • 3
    What do you mean when you say "A full cycle"? – Alex Mar 12 '19 at 19:51
  • Im using 3.7! The task runs everyday, and after 7 hours it runs again. When a new day starts it ends the cycle and start all over. – filipe Mar 12 '19 at 19:55
  • How many random numbers are generated in “one cycle”? – deceze Mar 12 '19 at 20:00
  • It runs 4 times, 00, 7 am, 2 pm, 9 pm. Generates one number at time from 1 to 1442 – filipe Mar 12 '19 at 20:02
  • The docs indicate that the current system time is used as the seed if one is not supplied directly. However the current system time one day later should be different so you should not generate an identical string of numbers assuming your clock is working properly. You can specify a seed directly, but if you use the same integer every time you will be guaranteed to get the same string of numbers. Are you sure your system clock is working properly? – Alex Mar 12 '19 at 20:05
  • Are you using `random.seed()` anywhere? If so, what value do you give it? – kichik Mar 12 '19 at 21:36
  • should i be using random.seed() wouldnt that be irrelevant given the function would be called at the same time everyday if its based on the clock ? – filipe Mar 12 '19 at 23:08
  • It is based on system time so even if it runs at the same time of day, the system time will be different. Using random.seed() allows you to specify a seed integer instead to generate deterministic random numbers repeatably. – Alex Mar 13 '19 at 11:20

2 Answers2

0

Assuming the instance is launched from the same image each time the entropy sources available to the machine will be identical. e.g. system clock, PID, hardware state

You could query some external source e.g. https://pypi.org/project/ntplib/ and use it as a seed to random.seed(). Note one caveat with using the time alone as a seed, is its predictable based on your given schedule.

Alternativley, rewrite the randHosp function as an AWS Lambda function

See this article for a similar discussion: https://security.stackexchange.com/questions/90101/dev-random-in-ec2-cloud

stacksonstacks
  • 8,613
  • 6
  • 28
  • 44
  • The question never indicates that the instance is being launched every time it is run. The job could just be running at fixed interval on a persistent instance. The author should clarify, because if that is the case, this answer may not apply. Although I find the idea of a lambda function to be a great suggestion. – Alex Mar 12 '19 at 20:21
  • It is an persistent instance, running at an interval time. – filipe Mar 12 '19 at 23:09
0

According to this answer you can generate more random for your randoms with

random.SystemRandom().randint(1, 1442)

which doesn't rely on the system clock for seeding, if that is indeed the issue. You might try it and see, if it doesn't resolve it then you know that's not the problem

G. Anderson
  • 5,815
  • 2
  • 14
  • 21