11/18/2010 9:17 PM | |
Posts: 11 Rating: (0) |
It's a great idea but I have a new one with a counter, if I figure it out I will post here, thanks |
This contribution was helpful to1 thankful Users |
4/13/2011 4:59 PM | |
Joined: 1/30/2006 Last visit: 9/11/2024 Posts: 104 Rating: (16) |
I apologize for such a late reply on this topic, but there may well be many who would benefit from a simple random number generator algorithm. The original poster's additional requirement was that the random number generator be called from a cyclic interrupt. This requirement presents two issues: 1) Interrupts usually need to be small in terms of code andcomputation time so that they do not interfere with the normally-running operations or overflow the interrupt call stack. So the random generator we use must be quickly executable. 2) Since the interrupt is cyclic, using a time-based random number generator is not likely to produce a very good set of pseudo-random numbers, i.e. the random numbers will be easily predictable since they are a result of periodic readings of the system clock. Thus, we require a random generator that does not repeatedly rely on the clock. On the other hand, a real-time clock can be a good way to set initial condition of the random number generator, also known as the seed value. One simple way to produce random numbers is with a method known by the term, linear congruence, which uses a simple, iterative formula: x[n+1] = (a * x[n]) mod m where n is simply the index of each successive random number generated. Since each new pseudo-random number is based on the last, the programmer needs only to store the previous value. Of course, coefficients "a" and "m" must be carefully chosen, but these may be "hard-coded". For a DINT (32-bit), I would suggest the following values: a = 16807 (the multiplier) m = (2^31) - 1 = 2147483647 (the modulus, a Mersenne Prime) x[0], the initial or seed value,should be an integer greater than zero The resulting pseudo-random numbers have a period of 2^31 before the user would see a repeat value. All of the resulting values are positive, and so if the desired random numbers must include negatives, the user should add an appropriate negative value to shift the results. Also, if an effective range of 0 to 2147483647 is too high, a suitable divisor may be selected to get the proper range. Actually, there are numerous possibilities for the coefficient selections, which one may see at http://en.wikipedia.org/wiki/Linear_congruential_generator However, keep in mind that not all of these possibilitiesare for 32-bit results. Something of which to be mindfulbefore implementing this solution: while this method is computationally efficient and therefore ideal for interrupts, it may not be of sufficient quality for randomness, although better than periodic clock reading. For example, if one is attempting to do cryptography (on a PLC???), this is not an adequate random number solution. Then again, as unlikely as cryptography is on a PLC, it seems to me even more unlikely to attempt something like that within interrupts. For something so sophisticated, the reader may be interested in the Mersenne Twister algorithm. . .if the PLC has lots of time for calculation. Respectfully submitted, The Captive Engineer |
This contribution was helpful to3 thankful Users |
11/19/2015 5:03 PM | |
Joined: 9/8/2009 Last visit: 7/17/2023 Posts: 1410 Rating: (153) |
I do use one pseudo random generator. The original was in C language, but this is exported from TIA v13 and it has the original extension .SCL. I have changed to txt to be able to publish here. ir1, jr1, kr1 are integers and they have initial values set to 111,222,333 at boot, they could be also stored in DB and never initialized again. AttachmentRand.txt (1545 Downloads) |
Last edited by: Marko Bursic at: 11/19/2015 5:05:13 PM |
|
This contribution was helpful to5 thankful Users |
11/19/2015 5:12 PM | |
Joined: 9/8/2009 Last visit: 7/17/2023 Posts: 1410 Rating: (153) |
And second function that converts uniform distribution to gaussian distribution. Multiplying and offseting you can have a certain distribution of numbers that suit standard deviation and mean value like a simulated product. I do use this for testing the sorting algorithm that distributes items with different weight. AttachmentRandGauss.txt (1125 Downloads) |
This contribution was helpful to3 thankful Users |
Follow us on