Jump to content

(Pseudo) Random Number Generation


Recommended Posts

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

  • Upvote 1
Link to comment
Share on other sites

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

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 by Oath
Posted too early! (and correcting myself)
Link to comment
Share on other sites

  • 2 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.