11/17/2010 4:42 PM  
Joined: 3/22/2010 Last visit: 12/2/2011 Posts: 11 Rating: (0) 
HI: 
11/17/2010 8:45 PM  
Joined: 7/7/2010 Last visit: 9/25/2018 Posts: 6393 Rating: (1025) 
This is definitely pseudorandom, but it (see attached image)does produce seemingly random values within that range. You might want to run it for a while, collecting data into buckets to see how well the values are distributed  if it matters. To test it in human terms, put the RND value in a watch table, add a TON with 1s time, and the TON.Q to run the RANDOM FC code in the image. 
Helpful? Rate it. 

11/18/2010 6:05 PM  
Joined: 3/22/2010 Last visit: 12/2/2011 Posts: 11 Rating: (0) 
Fine, it works, but, this is a good test question: How could I make a random generator for a cyclic interrupt? 
11/18/2010 6:15 PM  
Joined: 7/7/2010 Last visit: 9/25/2018 Posts: 6393 Rating: (1025) 
I'm sorry, I stopped taking tests a long time ago Perhaps you could post the math formula(s)you are attempting to accomplish. As for generating cyclic interrupts, you could look at the least significant digit (it will be 09), drive an output if it's a specific value or set of values. Wire that output to an input you use to drive a cyclic hardware interrupt. 
Helpful? Rate it. 

11/18/2010 9:17 PM  
Joined: 3/22/2010 Last visit: 12/2/2011 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 
4/13/2011 4:59 PM  
Joined: 1/30/2006 Last visit: 10/13/2017 Posts: 100 Rating: (12) 
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 normallyrunning 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 timebased random number generator is not likely to produce a very good set of pseudorandom 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 realtime 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 pseudorandom 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 "hardcoded". For a DINT (32bit), 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 pseudorandom 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 32bit 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 to1 thankful Users 
4/13/2011 6:42 PM  
Joined: 9/23/2005 Last visit: 9/24/2018 Posts: 3706 Rating: (938) 
Hi, to better understand your case, why do you need a random number? 
Denilson Pegaia 

4/13/2011 11:11 PM  
Joined: 1/30/2006 Last visit: 10/13/2017 Posts: 100 Rating: (12) 
The original poster, Danielinpayatronico, indicated that the random value was needed to identify a servo valve within a system. How this would actually work, I know not. I can suggest a couple other possibilites for random numbers. A simulationwith limited calculations might possibly be performed inside a PLC, and simulations often require random values for operating. Perhaps another use would be for a signal processing technique called dithering. In dithering, a slowly changing analog signal has a small amount of random noise added to it. In calculations for the analog signal over time, the addition of a little noise can sometimes help to undo the effect of least significant bit losses in aggregate computations. Surely there are other imaginative uses for random numbers in a PLC. Best regards, The Captive Engineer 
11/19/2015 5:03 PM  
Joined: 9/8/2009 Last visit: 9/24/2018 Posts: 1278 Rating: (118) 
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 (726 Downloads) 
Last edited by: Marko Bursic at: 11/19/2015 5:05:13 PM 

This contribution was helpful to1 thankful Users 
11/19/2015 5:12 PM  
Joined: 9/8/2009 Last visit: 9/24/2018 Posts: 1278 Rating: (118) 
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 (478 Downloads) 
Follow us on