﻿ PLC Programming: Random Number Generator

## Industrial Training - The Best for Less

﻿ # PLC Programming: Random Number Generator

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

## A universal PLC programming example of how to generate a truer exponential random number. 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 a 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.

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 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 Rung 2:

The time [frequency] the program will grab a random number from one of the two generators above. ### 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 Rung 4:

Select the random number from the 'per scan cycle' (MOV) random number generator. #### PLC Rung 5:

Select the random number from the 'multiple times per scan cycle' (CNT) random number generator. 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.