Oath Posted August 16, 2022 Report Share Posted August 16, 2022 I'm in the process of learning PLC programming, specifically in using the .NET Communications Driver to collect live-data from Vision PLCs (among other various tasks), and was wanting a way to generate some random data on the PLC for dynamic testing. What I found was a lack of a built-in random functions! So, for both the fun and functionality, I decided to implement a Shift-Register Generator for producing 32-bit Double-Word Pseudo-Random numbers (what a mouthful) based on the xorwow generator ( https://en.wikipedia.org/wiki/Xorshift#:~:text=four%20words%20*/%0Auint32_t-,xorwow,-(struct%20xorwow_state ). The project is implemented on a V570 (as it was what we had easily at-hand), but the underlying ladder logic should be easily portable to any similar hardware, so long as there are 10 DW's available for storage and output. An example of collecting just an integer value from the double word is also included. As I am still learning, please don't hesitate to point out suggestions or corrections within the program itself, in either style or implementation. Thanks! RandGen.vlp 1 Link to comment Share on other sites More sharing options...
MVP 2023 kratmel Posted August 16, 2022 MVP 2023 Report Share Posted August 16, 2022 Great job! Thanks for fine comment and links to publications about random number generation. Link to comment Share on other sites More sharing options...
AlexUT Posted August 17, 2022 Report Share Posted August 17, 2022 Oath, I join to kratmel and thank you too! By the way, Unitronics provide Random Number Generator example for V280 and V570 PLCs in directory: C:\Program Files (x86)\Unitronics\Unitronics VisiLogic_C\Examples\Version 900\Project examples\ladder\ There are many other examples. Unitronics has special page "Tools & Applications from the Unitronics Community", where community members can share his examples/utility: https://www.unitronicsplc.com/support-tools-and-applications/ B.R. Link to comment Share on other sites More sharing options...
Oath Posted August 17, 2022 Author Report Share Posted August 17, 2022 (edited) Thank you both for taking a look! @AlexUT I'd seen the Park-Miller RNG floating around (though I didn't realize it was in the examples!), but I wanted to be able to generate up-to 32-bits worth of data, (duh - the Parker-Miller does produce 32-bit) and the xorwow function looked pretty straight-forward to implement(plus I wanted to play around with the bit-shifting and XOR functions, which were very easy to use, which was nice). From a technical note, the Parker-Miller cycles after 232-1 uses while the xorwow cycles after 2192-232 uses (not that I need anywhere NEAR that many randomly generated numbers, mind!), so I was going for a more robust system, where you might be generating multiple times a cycle, for a really long time (though both PRNG's are likely overkill in most cases). The thing I found most interesting about the xorwow, though, is that by increasing or decreasing the number of states, you can increase or decrease your cycle time, respectively, as the states shifting like they do mimics a really big bit-shift. I'll have to more carefully peruse the example files though - there is certainly a lot of things there! Edited August 17, 2022 by Oath Posted too early! (and correcting myself) Link to comment Share on other sites More sharing options...
kvlada Posted November 2, 2022 Report Share Posted November 2, 2022 Interesting, thank you for this implementation. I usually just take last few digits from system cycle counter. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now