View Full Version : Random Numbers....
May 20th, 2002, 04:31 AM
I have been trying to make a random number generator using
RSLogix 5 and 500 but can seem to get it completely random. I have tried using different timers together but it still comes up with a pattern. I would appreciate any help. Thanks..
May 20th, 2002, 07:37 AM
Try using something which varies, not according to your ladder but somtething else ... like the RTC (real time clock).
You can parse and add all the single digit and then end-up with a number which will be the result of something which is variable.
IMHO you cannot get a true random number in a PLC but you can get soething looking very much so!
Use a long timer, 1000 seconds
Every scan, move the acc. value to a register
Add it to the RTC seconds value registers into a third register
Multiply this register with the minute value
Parse the digits,
Multiply them with an interrupted value, corresponding with the scan time...
And so on...
'till your getting a random looking value
May 20th, 2002, 10:22 AM
Computers are not known for being able to generate true random numbers in the first place.
I believe "random numbers" are generated by moving a pointer in a table of "random" digits. I think there is some specific irrational number that is used to generate that table. To make things appear even more random, you use a timer to move the pointer around. When you're ready to access the table of "random" values, the pointer could be anywhere.
PLCs don't have random number functions because there's not much need for them. Usually we like to know how our controllers are going to behave, especially when they're connected to a few thousand dollars worth of hardware.
May 20th, 2002, 02:47 PM
A couple of years agoo, I had the same problem. I transferred the solution used in an old Sinclair Spectrum to a Siemens S5 PLC. Basically this formula for an 16-bit integer random number is as follows:
1. Take a seed (usually the last random number generated, the RTC gives a better result)
2. Increment the seed by 1
3. Multiply by 75
4. Subtract the high byte from the low byte
5. Decrement this result by 1, which gives the new random number.
Originally in the Spectrum the formula was ((Seed+1)*75) MOD 65537 -1.
I found that my result, which gives a pseudo-random sequence of 65536 numbers, worked quitte well. I hope it does so for you too!
Jean Pierre Vandecandelaere
Instructor PLC - SCADA
May 20th, 2002, 02:50 PM
I'll try it ASAP. Just for fun.
May 20th, 2002, 04:12 PM
I will try these suggestions and see how it turns out.
Thanks for the help.
May 21st, 2002, 12:09 AM
Sorry, but I made a mistake in the steps to calculate the random number. Step 4 should state:
4. Subtract the high WORD from the low WORD
Sorry, if I confused you guys!