Random Number Generators Suitable for PLCs

http://www.codeproject.com/KB/recipes/SimpleRNG.aspx

I came across this simple version a couple of years ago. It is simple enough to implement in a PLC which is why it is so appealing. I use it now but if something better comes along I will post a link to it too.

I wrecked my head over this for a week... Needed one to issue tickets with a PIN code after payment. It needed to be a 6 digit number.

How I did it in the end (and i'd appreciate any criticism) was :

1. First scan load 2 INTs with different values greater than 100.
2.Use a very fast pulse to increment each INT every scan.
3. Ensure each INT never goes above 999 and never below 100.
4. Convert both INTs to string and concatenate.
5. Back to INT.
6. Do some manipulation of both int registers some days using clock seconds based on customer purchase events.

Fairly random i think!

Would love to hear opinions on an algoritim to generate 6 digit random numbers.Also a neat way to ensure each number is unique. All I can get online is from Java, C/C++ etc making use of RAND() functions which of course arent in my instruction set.

Thanks
 
I tried this formula for A2 in a spreadsheet then filled down.

MOD(A1+512927;900000)+100000

I chose 512927 as a prime more than half of the range (900000). We generate a new value by adding a prime and taking the MOD of 900000 to get 0 - 899999. The we add 100000 to get 100000 - 999999. I think adding a prime and then MODding should cycle through all values before repeating. The first 20 or so look fairly random to my eye. Obviously they aren't really random but relatively non guessable to a casual observer is what I'm after.
 
If you looked the link you would see that some effort has been put into the SimpleRNG so that it passes the DIEHARD battery of test that is used to test random number generators. I wouldn't use a MOD instruction to generate random numbers for a security system.
The second function GetUniform returns a number between 0 and 1. Any RNG function that does so could have been used in a fashion similar to what Bernie did.
SixDigitNumber:=REAL_TO_DINT(GetUniform()*900000)+100000;
 
Peter: a question about the 'MWC' link in that link you posted. Concerning MWC it says (about half way down the text):
The MWC generator concatenates two 16-bit multiply-with-carry
generators, x(n)=36969x(n-1)+carry, y(n)=18000y(n-1)+carry
mod 216, has period about 260 and seems to pass all tests of
randomness. A favorite stand-alone generator---faster than KISS,
which contains it.
Is the 'mod 216" a typo? Or is 'period about 260' correct? If I understand the terms correctly wouldn't a 'period about 260' mean that it starts repeating after 260 random numbers?

[Edit: - and yes my method provides numbers, definitely not random as subtracting successive numbers shows only two possibilities, failing any possible test for randomness.]
 
Last edited:
Peter: a question about the 'MWC' link in that link you posted. Concerning MWC it says (about half way down the text):
Is the 'mod 216" a typo? Or is 'period about 260' correct? If I understand the terms correctly wouldn't a 'period about 260' mean that it starts repeating after 260 random numbers?
The simple answer is I don't know. I would have to try that particular random number generator out to see. I do know the code on the first HTML pages is simple and it works. The MWC link points to 5 other random number generators and I didn't bother to try them.
 

Similar Topics

Hey folks, I was just wondering what thoughts were about a completely 100% random number generator. I looked on The Google but didn't really open...
Replies
54
Views
16,560
Hello, Could somebody please help me out with coding a 'Random number generator' in vijeo citect. For example if i create an analog Local...
Replies
3
Views
3,772
i need to set a unit up with random number generation to enable the timing on a switch to be 'erratic', does anybody know if its possible? i need...
Replies
16
Views
5,595
Hi,Does anybody know if there are any routines in unity pro that produce random numbers like the routine rand() in C ?
Replies
1
Views
4,035
Ive got a client that needs random numbers generated for a security system. Just want to know if any 1 has some ladder or ST software or scripts...
Replies
11
Views
8,899
Back
Top Bottom