in c#, how to independently guarantee that two machines will not net generate the same random number?

167 views Asked by At

Suppose two machines are running the same code, but you want to offset the timing of the code being run so that there's no possibility of their not running simultaneously, and by simultaneously I mean not running within 5 seconds of each other.

One could generate a random number of seconds prior to the start of the running code, but that may generate the same number.

Is there an algorithm to independently guarantee different random numbers?

5

There are 5 answers

1
Alexei Levenkov On

As pointed in comments/other answer true random can't really provide any guarantees of falling into particular ranges when running in parallel independently.

Assuming your goal is to not run multiple processes at the same time you can force each machine to pick different time slot to run the process.

If you can get consensus between this machines on current time and "index" of machine than you can run your program at selected slots with possible random offset withing time slot.

I.e. use time service to synchronize time (default behavior for most of OS for machines connected to pretty much any network) and pre-assign sequential IDs to machines (and have info on total count). Than let machine with ID to run in time slot like (assuming count < 60, otherwise adjust start time based on count; provide enough time to avoid overlaps when small time drift happens between time synchronization interval)

(start of an hour + (ID*minutes) + random_offset (0,30 seconds))

This way no communications between machines is needed.

1
David Arno On

Have both app read a local config file, wait the number of seconds specified and then start running.

Put 0 in one, 6+ in the other. They'll not start within 5 seconds of each other. (Adjust the 6+ as necessary to cater for variations in machine loads, speeds etc.)

5
Kyle W On

In order to guarantee that the apps don't run at the same time, you need some sort of communication between the two. This could be as simple as someone setting a configuration value to run at a specific time (or delay by a set amount of seconds if you can guarantee they will start at the same time). Or it might require calling into a database (or similar) to determine when it is going to start.

0
Dan Field On

It sounds like you're looking for a scheduler. You'd want a third service (the scheduler) which maintains when applications are supposed to/allowed to start. I would avoid having the applications talk directly to each other, as this will become a nightmare as your requirements become more complex (a third computer gets added, another program has to follow similar scheduling rules, etc.).

Have the programs send something unique (the MAC address of the machine, a GUID that only gets generated once and stored in a config file, etc.) to the scheduling service, and have it respond with how many seconds (if any) that program has to wait to begin its main execution loop. Or better yet, give the scheduler permissions on both machines to run the program at specified times.

You can't do this in pure isolation though - let's say that you have one program uniquely decide to wait 5 seconds, and the other wait 7 seconds - but what happens when the counter for program 2 is started 2 seconds before program 1? A scheduler can take care of that for you.

0
Touch Cat Digital Inc. On

Not really an algorithm but you could create two arrays of numbers that are completely different and then grab a number from the array (randomly) before the app starts.

What is the penalty for them running at the same time?

The reason I ask is that even if you offset the starting time one could start before the other one is finished. If the data they are processing grows then this gets more likely as time goes on and the 5s rule becomes obsolete.

If they use the same resources then it would be best to use those resources somehow to tell you. E.g. Set a flag in the database, or check if there is enough memory available to run.