You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

Reply
 
Thread Tools Display Modes
Old July 28th, 2020, 05:02 PM   #46
agarb
Member
United States

agarb is offline
 
Join Date: May 2006
Location: USA
Posts: 301
I just did a similar thing on an 8 bit microcontroller with very limited resources a few weeks ago where I needed a seemingly random delay of 2 to ~10 minutes. If pseudo-random is good enough then keep reading… here are my code comments.


/* See: https://en.wikipedia.org/wiki/Linear...tial_generator
rand = (rand * a + c) mod m <--- this is the general form of the random number generator.

Now you must pick a, c, and m

For this application, we want a delay of 2-10 minutes which is 120-600 seconds.
So let's use 109 for a and then add 89 which is c.
M will be 512 as this is a power of 2 and allows a simple shift to be used.
Now the output ranges from 0-511


Finally add 120 to provide a delay of 120-631 seconds.

Final form of equation: rand = (rand * 109 + 89) %512 + 120

I verified "random" distribution by histogram in Excel.
*/

Does your PLC-5 have the CPT and MOD instructions? If you don’t have MOD then you will have to do some bit shifting. Or maybe look at the math overflow registers and see if the remainder shows up there like it does in the SLC. Its been a long time since I messed with the PLC-5 but I think I have one around here somewhere...

To get 0-9999 then m must be 10000.

Try 109 for a and 89 for b.

Also, you need to ‘seed’ the calculation by moving something into "ran" on the first scan if you want the sequence to start in a different place each time.

To do this, maybe use the seconds from time of day. Or an analog value from a transducer that is always changing to generate the seed.

I’m not as smart as Peter and others but this is where I would start. Assuming “pseudo-random” is good enough…
  Reply With Quote
Old July 28th, 2020, 06:05 PM   #47
Phrog30
Member
United States

Phrog30 is offline
 
Join Date: Dec 2006
Location: Montgomery, Alabama
Posts: 657
Quote:
Originally Posted by the_msp View Post
Cool video, thanks for sharing
  Reply With Quote
Old July 28th, 2020, 08:13 PM   #48
Peter Nachtwey
Member
United States

Peter Nachtwey is online now
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 7,185
Quote:
Originally Posted by drbitboy View Post
While that may be adequate in some cases, there is no way that is correct for every application.
Define what is incorrect for any application?


Quote:
No, the number would be 16, not 2, and actually 32 with the Marsaglia algorithm.
Two BSLs or BSRs are required only if a new random number is needed every scan. They would alternate shifting bits because a one shot is required.


A new random number is generated every time the bits are shifted one bit. See this example
https://www.cs.princeton.edu/courses...ents/lfsr.html
The for loop does 10 iterations or steps and generates 10 random numbers.


You are looking for fault where there isn't any. Who is the troll now?





Quote:

Anyway, like I wrote several posts ago, AND/DIV/AND would be better.
The pesky sign bit gets in the way. If dividing by 2 to shift right 1 the sign bit doesn't change. Shifting left by multiplying by 2 will cause the over flow bit to come on. Where is the XOR?

The BSL and BSR don't care about sign bits and more than 16 bits can be shifted at a time.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon
  Reply With Quote
Old July 28th, 2020, 09:11 PM   #49
I_Automation
Member
United States

I_Automation is offline
 
I_Automation's Avatar
 
Join Date: Jun 2020
Location: Detroit, Michigan USA
Posts: 154
Forgetting about the HOW



I would like to ask WHY would a PLC program need a random number generated?


I have been in PLC's since the mid 90's and every value I have encountered and programmed has been a very specific value. I can't see a need for a truly random number in a PLC program.


If anyone has a real need for this, outside of an academic project, please reply.
  Reply With Quote
Old July 28th, 2020, 09:37 PM   #50
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 1,236
Quote:
Originally Posted by I_Automation View Post
If anyone has a real need for this, outside of an academic project, please reply.



Yes, I think almost every PLC system IRL (In Real Life) needs less noise, not more.


As you say, academic projects, e.g. programming the simple simon game, are usually a programming exercise, but it could be used in a carnival environment Same with whack-a-mole, or a mechanical bull.


Another use for RNG is in testing the primary algorithm using a simulation with noise. My brother and I coded a Gaussian-disttributed RNG (Random Number Generator) to show that, on a noisy PV input to a PID, making the deadband too small, or not using a deadband at all, adds to the noise in the controlled system, because the PID ends up chasing the noise. That was an academic exercise to be sure. But if someone actually wanted to code a simulation of a process e.g. bottles showing up on a conveyor line, to test the performance of their control system before the line was commissioned, that is not academic.



Stepping back a bit, what is most interesting to me is that, in every thread or online discussion about RNGs I have seen, it never goes more than a handful of responses before someone asks why. It just goes to show how universal is the tendency to consider oneself normative: "I would never need an RNG in a PLC, so I am all but certain no one else would/could/should ever need one."
  Reply With Quote
Old July 28th, 2020, 09:51 PM   #51
agarb
Member
United States

agarb is offline
 
Join Date: May 2006
Location: USA
Posts: 301
Quote:
Originally Posted by agarb View Post
I just did a similar thing on an 8 bit microcontroller with very limited resources a few weeks ago where I needed a seemingly random delay of 2 to ~10 minutes. If pseudo-random is good enough then keep reading… here are my code comments.


/* See: https://en.wikipedia.org/wiki/Linear...tial_generator
rand = (rand * a + c) mod m <--- this is the general form of the random number generator.

Now you must pick a, c, and m

For this application, we want a delay of 2-10 minutes which is 120-600 seconds.
So let's use 109 for a and then add 89 which is c.
M will be 512 as this is a power of 2 and allows a simple shift to be used.
Now the output ranges from 0-511


Finally add 120 to provide a delay of 120-631 seconds.

Final form of equation: rand = (rand * 109 + 89) %512 + 120

I verified "random" distribution by histogram in Excel.
*/

Does your PLC-5 have the CPT and MOD instructions? If you don’t have MOD then you will have to do some bit shifting. Or maybe look at the math overflow registers and see if the remainder shows up there like it does in the SLC. Its been a long time since I messed with the PLC-5 but I think I have one around here somewhere...

To get 0-9999 then m must be 10000.

Try 109 for a and 89 for b.

Also, you need to ‘seed’ the calculation by moving something into "ran" on the first scan if you want the sequence to start in a different place each time.

To do this, maybe use the seconds from time of day. Or an analog value from a transducer that is always changing to generate the seed.

I’m not as smart as Peter and others but this is where I would start. Assuming “pseudo-random” is good enough…

I just realized the original post was from 2004! I hacked together a quick PLC-5/20 program that seems to work. No doubt that with a bit of time it could be made more elegant.



I trended the random value for a minute and it was all over the place.
Attached Images
File Type: gif random.gif (33.3 KB, 6 views)
File Type: gif graph.gif (48.4 KB, 5 views)
  Reply With Quote
Old July 28th, 2020, 09:53 PM   #52
Phrog30
Member
United States

Phrog30 is offline
 
Join Date: Dec 2006
Location: Montgomery, Alabama
Posts: 657
Quote:
Originally Posted by agarb View Post
I just realized the original post was from 2004!
You are one of the few. Pete and the Dr still haven't realized that!
  Reply With Quote
Old July 28th, 2020, 10:13 PM   #53
agarb
Member
United States

agarb is offline
 
Join Date: May 2006
Location: USA
Posts: 301
Quote:
Originally Posted by I_Automation View Post
Forgetting about the HOW



I would like to ask WHY would a PLC program need a random number generated?


I have been in PLC's since the mid 90's and every value I have encountered and programmed has been a very specific value. I can't see a need for a truly random number in a PLC program.


If anyone has a real need for this, outside of an academic project, please reply.

Not a PLC program, but I needed a random delay last week for a prank I am working on with an AVR microcontroller. I'm making a miniature device that will randomly beep every 2-10 minutes. I had little circuit boards made that I plan to hide around a few coworkers desks and watch them hunt for the source the brief annoying beep. Think of that pesky smoke alarm battery dying when you are trying to go to sleep...

Runs for days on a CR2032 battery mounted on rear of board.
Attached Images
File Type: gif prank.gif (88.2 KB, 74 views)
  Reply With Quote
Old July 28th, 2020, 10:50 PM   #54
PLCnovice61
Member
Australia

PLCnovice61 is offline
 
Join Date: Apr 2015
Location: home
Posts: 256
l have just now use a RNG as an input to a motor Tacho counter on an Arduino, because without a valid pulse the Arduino would lock up, also with a RNG as the input l could confirm that it was working, instead of having a "const int" which l wouldn't be able to tell if my code was truly working.
Plenty of RNG's out there in the Arduino world as elaborate or as uncomplicated as you wont, just look into the .h .ccp files.

Re 2004, l would hope dr knows it was from then, as he was the one the went looking for it and put the first post in 16 years to the thread.

Last edited by PLCnovice61; July 28th, 2020 at 10:52 PM.
  Reply With Quote
Old July 28th, 2020, 11:54 PM   #55
Peter Nachtwey
Member
United States

Peter Nachtwey is online now
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 7,185
Quote:
Originally Posted by Phrog30 View Post
You are one of the few. Pete and the Dr still haven't realized that!
I realize that. I don't know how I missed this thread 15 years ago. I certainly knew about LFSRs back them since I used them about 28-30 years ago. I wasn't aware of George Marsaglia back then. It is all about being aware. I probably used something out of Numerical Recipes in C.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon
  Reply With Quote
Old July 29th, 2020, 08:41 AM   #56
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 1,236
Quote:
Originally Posted by Phrog30 View Post
You are one of the few. Pete and the Dr still haven't realized that!

Actually I did notice that early on, but what does it matter? It's an interesting topic. If you scroll up (or down) the first response asks why, less than an hour and a half after the OP in 2004.
  Reply With Quote
Old July 30th, 2020, 07:05 AM   #57
I_Automation
Member
United States

I_Automation is offline
 
I_Automation's Avatar
 
Join Date: Jun 2020
Location: Detroit, Michigan USA
Posts: 154
Maybe I worded the question wrong.

Why would a PLC need a "truly random" number generator.

For testing a PID, bottles on a conveyor simulation and beeping occasionally to annoy co-workers a basic random number that might have a pattern every few [timeframe] would still be good enough.

Even for testing bottles on a conveyor the number will be 0 or 1 and a XIC S;4/3 of the time base will be random enough, and if you want to see if a bottle shows up only every 1 second then XIC S:4/2 XIC S:4/7.

For a PID simulation as long as a repeat isn't an identical loop of values then getting a few numbers in a row the same a a few minutes ago shouldn't be an issue with testing.

The annoying beeper might have a future place in my laptop case, but an Arduino is not going to generate a truly random number.
  Reply With Quote
Old July 30th, 2020, 07:25 AM   #58
geniusintraining
Lifetime Supporting Member + Moderator
United States

geniusintraining is offline
 
geniusintraining's Avatar
 
Join Date: Jun 2005
Location: SC
Posts: 6,994
Quote:
Originally Posted by drbitboy View Post
It's an interesting topic.
Yep, most of the time I dont like to see them rehashed but I have been reading this one and its a good lesson and a lot to be learned, I have been trying to write one using a Click but not much luck for being random
__________________
www.PLCCable.com PLC Communication Cables, PLC Trainers, MicroLogix, ControlLogix, Siemens, Allen Bradley and more...OEM and aftermarket supplies... Aftermarket 1784-U2DHP Allen Bradley USB to DH+, new USB to 485 modbus
  Reply With Quote
Old July 30th, 2020, 09:22 AM   #59
drbitboy
Lifetime Supporting Member
United States

drbitboy is offline
 
drbitboy's Avatar
 
Join Date: Dec 2019
Location: Rochester, NY
Posts: 1,236
Quote:
Originally Posted by geniusintraining View Post
Yep, most of the time I dont like to see them rehashed but I have been reading this one and its a good lesson and a lot to be learned, I have been trying to write one using a Click but not much luck for being random


https://en.wikipedia.org/wiki/Linear...tial_generator


The trick is to handle the overflow, by ignoring it.





https://github.com/drbitboy/plc_rng
  Reply With Quote
Old July 30th, 2020, 10:27 AM   #60
Firejo
Member
United States

Firejo is offline
 
Firejo's Avatar
 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 1,428
Quote:
Originally Posted by the_msp View Post
Very cool video. Not sure how it ties into random numbers but I will be honest and say that I haven't been able to watch it to the end (yet).
__________________
Go Hawks!!!
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
S7 Random Number Generator RRobbins LIVE PLC Questions And Answers 14 July 8th, 2016 08:36 AM
Allen Bradley 1761-NET-ENI Jezz LIVE PLC Questions And Answers 11 August 17th, 2007 08:56 PM
S7-416-2 DP Fault - SFB20 sparky64 LIVE PLC Questions And Answers 8 July 30th, 2004 03:48 AM
Philosophical Illusion? Terry Woods LIVE PLC Questions And Answers 71 June 28th, 2004 08:41 AM
"Random" number generation ajbachhuber LIVE PLC Questions And Answers 9 September 10th, 2003 09:41 PM


All times are GMT -4. The time now is 07:17 PM.


.