I just did a similar thing on an 8 bit microcontroller with very limited resources a few weeks ago where I needed a seemingly random delay of 2 to ~10 minutes. If pseudo-random is good enough then keep reading… here are my code comments.
/* See: https://en.wikipedia.org/wiki/Linear_congruential_generator
rand = (rand * a + c) mod m <--- this is the general form of the random number generator.
Now you must pick a, c, and m
For this application, we want a delay of 2-10 minutes which is 120-600 seconds.
So let's use 109 for a and then add 89 which is c.
M will be 512 as this is a power of 2 and allows a simple shift to be used.
Now the output ranges from 0-511
Finally add 120 to provide a delay of 120-631 seconds.
Final form of equation: rand = (rand * 109 + 89) %512 + 120
I verified "random" distribution by histogram in Excel.
*/
Does your PLC-5 have the CPT and MOD instructions? If you don’t have MOD then you will have to do some bit shifting. Or maybe look at the math overflow registers and see if the remainder shows up there like it does in the SLC. Its been a long time since I messed with the PLC-5 but I think I have one around here somewhere...
To get 0-9999 then m must be 10000.
Try 109 for a and 89 for b.
Also, you need to ‘seed’ the calculation by moving something into "ran" on the first scan if you want the sequence to start in a different place each time.
To do this, maybe use the seconds from time of day. Or an analog value from a transducer that is always changing to generate the seed.
I’m not as smart as Peter and others but this is where I would start. Assuming “pseudo-random” is good enough…
/* See: https://en.wikipedia.org/wiki/Linear_congruential_generator
rand = (rand * a + c) mod m <--- this is the general form of the random number generator.
Now you must pick a, c, and m
For this application, we want a delay of 2-10 minutes which is 120-600 seconds.
So let's use 109 for a and then add 89 which is c.
M will be 512 as this is a power of 2 and allows a simple shift to be used.
Now the output ranges from 0-511
Finally add 120 to provide a delay of 120-631 seconds.
Final form of equation: rand = (rand * 109 + 89) %512 + 120
I verified "random" distribution by histogram in Excel.
*/
Does your PLC-5 have the CPT and MOD instructions? If you don’t have MOD then you will have to do some bit shifting. Or maybe look at the math overflow registers and see if the remainder shows up there like it does in the SLC. Its been a long time since I messed with the PLC-5 but I think I have one around here somewhere...
To get 0-9999 then m must be 10000.
Try 109 for a and 89 for b.
Also, you need to ‘seed’ the calculation by moving something into "ran" on the first scan if you want the sequence to start in a different place each time.
To do this, maybe use the seconds from time of day. Or an analog value from a transducer that is always changing to generate the seed.
I’m not as smart as Peter and others but this is where I would start. Assuming “pseudo-random” is good enough…