This is a very interesting demand. All the microcomputers, including PLC's are deterministic machines, therefore a "true" random generator is not possible for a PLC, like in a PC (random commands in any computer language does not give mathematically random results), however I will try to suggest an "almost random" (or pseudo-random, see www.random.org) generator: 1. Use the time interrupt facility of the PLC to increase a variable 1 at each 1 ms. Since the time interrupt is directly taken from the crystal inside the CPU, it can be assumed as constant. 2. Start a timer with time base of *100 ms* to say, 200 ms. Since the timers have a resolution limited to the time base, it will switch on 100 to 300 ms (or 150 to 250 ms, I am not sure). As far as I know, integrated timers are not tuned to the crystal, so there is a (random?) difference between timer counts and timed interrupt counts. 3. As the timer switchs on, set an immediate output which is connected to an interrupt input. By this interrupt input go and read the variable at step 1. Now you have an "almost random" number between 100 to 300 ms (or 150 to 250 ms, or a different figure, but nearly random). I assume the delays for the input and output are constant (or random again). 4. Reset the variable and the timer and start from step 1 for another random number. Another (and easier) alternative: 1. As above, but adjust the interrupt time close to your average scan time. 2. Increase another variable for 1 at each *scan*. Since the scan time for each command in your program is inaccurate (depending on the individual behaviour of the electrons - not tuned to the crystal again), after say each 1 second you will have a difference between the two variables, which I assume to be "almost random". Another alternative if the choice will be done externally: 1. Produce a table of random numbers in the PLC using the source I have given above. 2. Produce a pointer to show an element in the table and change it continuously 3. Stop the pointer as the external event (e.g., pressing a button). Please try these and inform us about the results. I am also curious about your application. Why do you need a random number? I always think that engineering is dealing with the predictable. I want to know unpredictable applications related to control engineering. I think there will be other (and probably better) ideas from the list. Best regards and good luck, Hakan Ozevin
> I'm looking for a random numbers generator for a A-B or > Siemens PLC without analog input. A cheating solution if you don't need this number too often is to create the fastest free-running timer the PLC offers (1000 or 100 cnts per second?). Then if you only needed this "psuedo-random number" once each few hours & it was triggered from an external event you'd have the appearance of randomness as sampled. Of course if this number was sampled based on some cycle of ladder rung scans, this would not work. Only if some external event is triggering its use. Regards Lynn August Linse, Senior Product Application Engineer 15353 Barranca Parkway, Lantronix Inc, Irvine CA 92618 [email protected]www.lantronix.com Tel: (949)300-6337 Fax: (949)453-7132
If you extend this technique by using multiple freerunning timers each with a different preset and each preset a prime number you can increase the aparent "randomness" of the generated number. Use different external events to trigger the sampling and as aux resets. Then on the sample trigger sample each of the timers and XOR them to generate your "random" number. Not perfect but good enough for most applications. I have used a similar method under dos to generate crypto cards for our comm section when I was in the Seabee reserve.
I posted a paper a few days ago on the PLC Archive that is used for simulation of PLC program controlled devices. It included a pseudo-random number generator that solves the equation: A(new) = A(old) + B + E + 1 where, after calculating the new A, each value is changed by copying E <= D D <= C C <= B B <= A A set of move instructions can do this job in a PLC. Of course, you need to do the moves in the correct order. I mentioned that a pseudo-random number is generated since the results are biased towards even or odd numbers depending on the constant added at the end of the equation. I found this in "The first book of KIM", a programming book for the KIM-1, a 6502 based processor evaluation board that came out in the late 1970s. The calculation was published well before that but the 1978 date is the last I could find. A sample program is listed in the appendix file also available at the PLC Archive. Regards, Russ Kinner AVCA Corporation Maumee, OH USA
Yet another solution: I have made pseudo-random numbers on a PLC by making a shift register with xor feedback in some selected positions if this register is long enough (for example 24 bits) AND the feedback mask is correctly selected it will not repeat itself for a really long time. I updated this each scan and for the application where I wanted it I used a timer to read the value, the values became different between two runs immediately. If you don't use a timer or an external event for "reading" you could xor-feed the register with one and by that make it really random. I could give you a "good" xor mask for any length up to 24 bits if you want one. /Johan Bengtsson ---------------------------------------- P&L, Innovation in training Box 252, S-281 23 H{ssleholm SWEDEN Tel: +46 451 49 460, Fax: +46 451 89 833 E-mail: [email protected] Internet: http://www.pol.se/ ----------------------------------------
Hi, It might be a good idea to test your results after you choose one of the suggested methods. Generate a sample (10,000 or more... More is better.) of pseudorandom numbers, build a histogram with narrow buckets in Excel, JMP, or other commercial software of your choice, and examine for repeated or missing values. Repeat several times, visually comparing the histograms. I have seen spikey and gap-toothed histograms result from not-so-random generators. Some resulted from the choice of seed value. Regards, John G. Boland, president VisiBit Corporation www.visibit.com One Parker Square Suite 408 2525 Kell Boulevard Wichita Falls, Texas 76308 940.322.9922 940.723.1478 fax
Well, (this might be far off or not) if you need TRULY random numbers (do you?) I think you have to check at least the following: For a big sample, all numbers should appear approx the same number of times (with some random variation of course) further: say that you generate random numbers between 0 and 9 then: 0 followed by 0 0 followed by 1 .... 9 followed by 8 9 followed by 9 should appear approx the same number of times, all 100 combinations. the same should be true for longer sequences too for this to work the working register would have to be a lot larger than the output register. btw, one "good" xor mask for a 24 bit shift register is 1000 0001 0000 0000 0000 0011 If you do this it will cycle tru all combinations except all ones, unless you feed it with some external signal too when it will go thru all combinations. /Johan Bengtsson ---------------------------------------- P&L, Innovation in training Box 252, S-281 23 H{ssleholm SWEDEN Tel: +46 451 49 460, Fax: +46 451 89 833 E-mail: [email protected] Internet: http://www.pol.se/ ----------------------------------------
I used a timer accumulator value, and multiplied by a flow transmitters value. The random number was selected when operator pressed start button so that helped even more. if you create a math function and use some weird part of the result like the remainder, that will give pretty 'random' looking numbers.
I did not need anything too fancy or large so I used one of the "Quick and Dirty Generators" in "Numerical Recipies in FORTRAN, second edition, Press et al, Cambridge, page 274."
I implemented it in a PLC5/40E using the CPT compute instruction. The equations look like this:
jran=mod(jran*ia+ic,im)
ran=float(jran)/float(im
A table lists several possible values of the constants. I just chose the first set:
im = 6075, ia = 106, ic = 1283
I used a CPT instruction with the destination N7:21 and the expression:
((N7:21 * 106) + 1283) MOD 6075
I then used a DIV instruction using N7:21, 6075, and destination F8:15. That generated my random number between 1 and -1. I then added that to my simulated process variable. The entire rung text is as follows: