vocational training courses

Industrial Training - The Best for Less

PLC Programming: Random Number Generator

  1. Home >
  2. articles >
  3. automation >
  4. plc programming random

A universal PLC programming example of how to generate a truer exponential random number.

 

PLC Programming: Random Number Generator

 

PLC Programming: Random Number Generator

 

Truer PLC Exponential Random Number Generator:

This PLC programming example is how to program in ladder logic, the logic to generate a random number in PLCs that do not have an RND function built-in. This particular example is PLC ladder logic needed to generate a truer random number. (Exponentially more random) There is an added feature too, random number range selection. The PLC example is also universal with all brands of PLCs after you make necessary address changes for your particular brand and model of PLC.

 

I was asked how to create a random number in an Allen Bradley/Rockwell legacy PLC (SLC 500). My first thought was this must have been asked and answered many times over the years in the main PLC discussion groups. To my surprise, I was only half right. After checking the top 3 PLC discussion boards, I found it had been asked a lot, but only one person had ever answered correctly, and he did not give details or an example. The correct answer given was “Use two independent timing cycles in PLC”.


So, I decided to not only answer the question in more detail, but provide example logic, a live video of it working, and this article to explain further. Now the question of ‘How to generate random number in a PLC program on a PLC with no random instruction or function?’, has been answered here.


The approach I took in this PLC programming example is to tap two different timing cycles in the PLC, generate a random number within a certain range for each, then alternate back and forth between the two generators when selecting a random number. Resulting in an exponentially more true random number being selected each time.

 

Programming Random Number Generator in PLC:

2 random number generators:

 

PLC Rung 0:

Rung zero below is the fist of two random number generators. This one cycles linearly once per scan cycle.
The lower limit for the range of random numbers is N7:2 (The upper range limit is set by C5:0 preset value.)


plc programming rung 0

 

PLC Rung 1:

This rung is random number generator number two. This one cycles linearly multiple times per scan cycle (once per subroutine). The upper range limit is set by C5:0 preset value. (The lower limit for the range of random numbers is N7:2 )
* Also this random number generator uses a counter instead of an add instruction like the previous one did. So that differentiates the timing cycle from the first generator too.
** We used SLC 500 status bit (S:33/9). An even more true random number could be obtained by using another random event in the process.


plc programming rung 1

 

PLC Rung 2:

The time [frequency] the program will grab a random number from one of the two generators above.


plc programming rung 2

 

Random number generator selector:

PLC Rung 3:

This standard toggle on-off logic rung is used to switch every other time to the other random number generator.


plc programming rung 3

 

PLC Rung 4:

Select the random number from the 'per scan cycle' (MOV) random number generator.


plc programming rung 4

 

PLC Rung 5:

Select the random number from the 'multiple times per scan cycle' (CNT) random number generator.


plc programming rung 5

 

 

Also, in this approach, I wanted to keep the program relatively small. But instead of selecting a random number once every second or another preset time, you could program to select a random number each time a random event occurs in the process. For example, select a random number to be used as a tracking number, every time a scrap part is detected (Which is a random event, picking a random number). Another example would be picking a random number at the end of each machine cycle. While the machine cycle may be close to the same amount of time (like the timer is in our example), in most cases it will be slightly different each cycle. Plus, it is adding yet a third time cycle to the random number selection, making it more random than the once per second solution. The programming variation and possibilities a programmer may choose are infinite.

 

 

Copyright © 2020 Business Industrial Network and https://bin95.com. All rights reserved.

Although we do encourage you to share a link to this article, as the more who read it, the more companies will save.

 

Also see ...

AB SLC500: Indirect Addressing Program Example

ST PLC programming examples