lotuseater

Random Number Generator

10 posts in this topic

I am curious if anyone knows a good way to create a random number between 1 and 13 using Logix 5000. I have seen the example on AB's website found here: http://rockwellautomation.custhelp.com/cgi...171&p_olh=0 I think I may be able to use that example with some additional scaling. But, does anyone have any other options? There is a specific instance I am wanting to use this for, if we need to get into additional details I will. Thanks, Jake

Share this post


Link to post
Share on other sites
How often do you need a number generated. There are several websites random.org for one which generate random sequences of numbers. A file of 1024 Dints would not take up much space in a CLGX 5000 and random.org could generate 1024 numbers 1 thru 13 in a random sequence. A pointer would give you the next number each time you need it. You might also be able to use an EWEB module and hit a web site and get the number for each need.

Share this post


Link to post
Share on other sites
I uploaded code for an Omron version in the Omron PLC code section based on the following Generates a Pseudo random number using the mixed congrential method. Formula r(i+1) = (a r + b) MOD N This will generate a random number between 0 & N-1 with N = 2 (pwr 20) = 1,048,576, a = 1909, b = 221,571 You will see that at the end if you need to get a number between say 1-100 , or 1-1000 then simply multiply the result by the upper range. http://forums.mrplc.com/index.php?autocom=downloads&showfile=161

Share this post


Link to post
Share on other sites
Never needed a random number generation in control, but it could be done with scan time bits, as the scan time in usecs keeps changing even for a static program running with no change of states. Even a periodic task takes different times for execution every scan. Since there is an inbuilt randomness in the scan times due to the hardware design, this would be the best inbuilt randomness that you can feed off to develop an algorithm for a rand generator. It depends on the characteristics and frequency of the random number you want to generate that would determine the applicability. I have developed a PID loop simulation algorithm in RLL. I used the trig functions with system times to generate a randomly fuctuating PV (scaled appropriately). The system seconds, minutes, hours etc. they keep changing, it is impossible to predict the result of the trigonometric functions at the system second level, as the scan times of periodic tasks are not perfect, hence you get a randomly fluctuating value. Of course in my case the frequency was to the tune of 10 Hz or so. If you need a higher frequency then you can of course try using the scan time bits directly.

Share this post


Link to post
Share on other sites
About random number it is detailed discussion in this topic http://forums.mrplc.com/index.php?showtopic=10073

Share this post


Link to post
Share on other sites
Thank You for the Replies. I'll do some more thinking on this when I get some time and see what I can come up with.

Share this post


Link to post
Share on other sites
On 10/04/2007 at 5:02 AM, Sleepy Wombat said:

I uploaded code for an Omron version in the Omron PLC code section based on the following Generates a Pseudo random number using the mixed congrential method. Formula r(i+1) = (a r + b) MOD N This will generate a random number between 0 & N-1 with N = 2 (pwr 20) = 1,048,576, a = 1909, b = 221,571 You will see that at the end if you need to get a number between say 1-100 , or 1-1000 then simply multiply the result by the upper range. http://forums.mrplc.com/index.php?autocom=downloads&showfile=161

This link is no longer available, Could you please re upload the program maybe.

Share this post


Link to post
Share on other sites

Moderators can fix the links but this one happens to be in the downloads section here at MrPLC.

 

Share this post


Link to post
Share on other sites

thank you for reporting broken link.

btw. I do not see much of a problem since formula is posted so one can implement it in any programming language.

I just did it in excel:

1. put some value in A1 cell as N. this is upper limit for random value. if result is expected to be in range 0-100, set N value to 100

2. put some seed value in A2 cell. pseudo random means values are not really random (eventually repeat themselves). in this case any value in range will do. if unsure leave it at zero or N or anywhere in between

3. in cell  A3 enter formula =MOD(1909*A2+221571,$A$1)

4. click on cell A3, then click on black dot in lower right corner and drag it down some 100 cells or so 

5. check for repetitions. random function is only as good as absence of patterns in it's outputs. I notice that factors A and B have a major influence on repetitions and proposed values work but result in very short non-repeating sequence. here are some examples:

psng.png

psng2.png

Share this post


Link to post
Share on other sites

here is modified PSNG using XOR. I artificially limited it to 16-bit. graphed data is from B column. nice thing is that values in A2,A3,A4 have far less impact on non-repeated sequence length. Value A in cell A2 need to be some "Swiss cheese" bit pattern, B need to be greater than 1 but you can choose values pretty much at will...

 

psng_with_XOR.png

Share this post


Link to post
Share on other sites

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