Random number generator for PLC5

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…
 
While that may be adequate in some cases, there is no way that is correct for every application.
Define what is incorrect for any application?


No, the number would be 16, not 2, and actually 32 with the Marsaglia algorithm.
Two BSLs or BSRs are required only if a new random number is needed every scan. They would alternate shifting bits because a one shot is required.


A new random number is generated every time the bits are shifted one bit. See this example
https://www.cs.princeton.edu/courses/archive/fall10/cos126/assignments/lfsr.html
The for loop does 10 iterations or steps and generates 10 random numbers.


You are looking for fault where there isn't any. Who is the troll now?





Anyway, like I wrote several posts ago, AND/DIV/AND would be better.
The pesky sign bit gets in the way. If dividing by 2 to shift right 1 the sign bit doesn't change. Shifting left by multiplying by 2 will cause the over flow bit to come on. Where is the XOR?

The BSL and BSR don't care about sign bits and more than 16 bits can be shifted at a time.
 
Forgetting about the HOW



I would like to ask WHY would a PLC program need a random number generated?


I have been in PLC's since the mid 90's and every value I have encountered and programmed has been a very specific value. I can't see a need for a truly random number in a PLC program.


If anyone has a real need for this, outside of an academic project, please reply.
 
If anyone has a real need for this, outside of an academic project, please reply.




Yes, I think almost every PLC system IRL (In Real Life) needs less noise, not more.


As you say, academic projects, e.g. programming the simple simon game, are usually a programming exercise, but it could be used in a carnival environment Same with whack-a-mole, or a mechanical bull.


Another use for RNG is in testing the primary algorithm using a simulation with noise. My brother and I coded a Gaussian-disttributed RNG (Random Number Generator) to show that, on a noisy PV input to a PID, making the deadband too small, or not using a deadband at all, adds to the noise in the controlled system, because the PID ends up chasing the noise. That was an academic exercise to be sure. But if someone actually wanted to code a simulation of a process e.g. bottles showing up on a conveyor line, to test the performance of their control system before the line was commissioned, that is not academic.



Stepping back a bit, what is most interesting to me is that, in every thread or online discussion about RNGs I have seen, it never goes more than a handful of responses before someone asks why. It just goes to show how universal is the tendency to consider oneself normative: "I would never need an RNG in a PLC, so I am all but certain no one else would/could/should ever need one."
 
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…


I just realized the original post was from 2004! I hacked together a quick PLC-5/20 program that seems to work. No doubt that with a bit of time it could be made more elegant.



I trended the random value for a minute and it was all over the place.

random.gif graph.gif
 
Forgetting about the HOW



I would like to ask WHY would a PLC program need a random number generated?


I have been in PLC's since the mid 90's and every value I have encountered and programmed has been a very specific value. I can't see a need for a truly random number in a PLC program.


If anyone has a real need for this, outside of an academic project, please reply.


Not a PLC program, but I needed a random delay last week for a prank I am working on with an AVR microcontroller. I'm making a miniature device that will randomly beep every 2-10 minutes. I had little circuit boards made that I plan to hide around a few coworkers desks and watch them hunt for the source the brief annoying beep. Think of that pesky smoke alarm battery dying when you are trying to go to sleep...

Runs for days on a CR2032 battery mounted on rear of board.

prank.gif
 
l have just now use a RNG as an input to a motor Tacho counter on an Arduino, because without a valid pulse the Arduino would lock up, also with a RNG as the input l could confirm that it was working, instead of having a "const int" which l wouldn't be able to tell if my code was truly working.
Plenty of RNG's out there in the Arduino world as elaborate or as uncomplicated as you wont, just look into the .h .ccp files.

Re 2004, l would hope dr knows it was from then, as he was the one the went looking for it and put the first post in 16 years to the thread.
 
Last edited:
You are one of the few. Pete and the Dr still haven't realized that! :)
I realize that. I don't know how I missed this thread 15 years ago. I certainly knew about LFSRs back them since I used them about 28-30 years ago. I wasn't aware of George Marsaglia back then. It is all about being aware. I probably used something out of Numerical Recipes in C.
 
You are one of the few. Pete and the Dr still haven't realized that! :)


Actually I did notice that early on, but what does it matter? It's an interesting topic. If you scroll up (or down) the first response asks why, less than an hour and a half after the OP in 2004.
 
Maybe I worded the question wrong.

Why would a PLC need a "truly random" number generator.

For testing a PID, bottles on a conveyor simulation and beeping occasionally to annoy co-workers a basic random number that might have a pattern every few [timeframe] would still be good enough.

Even for testing bottles on a conveyor the number will be 0 or 1 and a XIC S;4/3 of the time base will be random enough, and if you want to see if a bottle shows up only every 1 second then XIC S:4/2 XIC S:4/7.

For a PID simulation as long as a repeat isn't an identical loop of values then getting a few numbers in a row the same a a few minutes ago shouldn't be an issue with testing.

The annoying beeper might have a future place in my laptop case, but an Arduino is not going to generate a truly random number.
 
It's an interesting topic.

Yep, most of the time I dont like to see them rehashed but I have been reading this one and its a good lesson and a lot to be learned, I have been trying to write one using a Click but not much luck for being random
 

Similar Topics

Hey folks, I was just wondering what thoughts were about a completely 100% random number generator. I looked on The Google but didn't really open...
Replies
54
Views
16,513
Hello, Could somebody please help me out with coding a 'Random number generator' in vijeo citect. For example if i create an analog Local...
Replies
3
Views
3,750
Hi,Does anybody know if there are any routines in unity pro that produce random numbers like the routine rand() in C ?
Replies
1
Views
4,030
Ive got a client that needs random numbers generated for a security system. Just want to know if any 1 has some ladder or ST software or scripts...
Replies
11
Views
8,853
Can anyone help me create a few ladder logic lines to randomly generate intergers btw 0-100. I have tryed loading the scan timer value, then ANDD...
Replies
16
Views
13,321
Back
Top Bottom