Algorthim/Logic to Generate 6 Digit Unique Random Numbers

Join Date
Jul 2007
Location
Kiruna
Posts
600
Hi there,

Has anyone ever had to generate random numbers for an application?

I can do this with an algorithim using the RTC as a seed but what I'm stumped on is a way to ensure each time I generate one its unique to the rest of the bunch. I'm not a statisically minded person so Its probably unlikely for a 6 digit number (100000-999999) to appear twice when I'll only ever require max 50 of these but none the less it could happen!

My thoughts are when I generate one I iterate through previously generated ones and ensure it dosent exist already. If it does generate again, but im sure theres a better way of doing things like this in the world of computing and authentication etc.

Any thoughts would be greatly welcomed.
 
"Random" qualified by "Unique" is not going to be fun to do in an PLC.

Why do you need random anyway? Why not just pick a starting point, and increment for every new number until rollover?

A long time back, I did a PLC based random number generator, in ladder logic, for AB and Panasonic PLC's. The code might be around here on the site somewhere, but it did not guarantee uniqueness.

I suppose, the best way would be to generate all 50 you need at one time, and store them in an array after determining uniqueness.

Generate one, it goes into RA[0]. The all following ones would have to loop through the existing array members to ensure uniqueness, if they are, add to the tail end of the array; If not, generate a new one and try.

The problem there, is that if you aren't careful about your random number generator, you could possibly end up in a tail-out dead end (same random repeating every time). Decent algorithms for RNG's don't have that problem though.
 
I have just grabbed the free running clock on an event (like a user PB) and scale it to my resolution/range. Works pretty good for most stuff.

I have also used excel/DDE/RSLinx Gateway to generate random numbers and fill up a file with them for a training exercise in class.
 
...when I'll only ever require max 50 of these but none the less it could happen!

Use a table then. Let Excel generate the numbers and store them in a DINT array (ControlLogix) or an L file(micrologix) or an F file (SLC,PLC/5)

edit: Use the excel function randbetween(100000,999999) to generate your six digit numbers. Since excel recalculates them then copy->paste spcial-> values to freeze the values while you transfer them to your table.
 
Last edited:
"Random" qualified by "Unique" is not going to be fun to do in an PLC.

Why do you need random anyway? Why not just pick a starting point, and increment for every new number until rollover?

A long time back, I did a PLC based random number generator, in ladder logic, for AB and Panasonic PLC's. The code might be around here on the site somewhere, but it did not guarantee uniqueness.

I suppose, the best way would be to generate all 50 you need at one time, and store them in an array after determining uniqueness.

Generate one, it goes into RA[0]. The all following ones would have to loop through the existing array members to ensure uniqueness, if they are, add to the tail end of the array; If not, generate a new one and try.

The problem there, is that if you aren't careful about your random number generator, you could possibly end up in a tail-out dead end (same random repeating every time). Decent algorithms for RNG's don't have that problem though.

I cant increment because I need random numbers to issue reciepts. The reciept will contain a 6 digit number which will allow the customer re entry. If I were to increment it then it would be very easy for someone to gain access to other peoples belongings.
On the issue of unique....if two numbers were the same and a customer entered their pin code, then 2 doors would open which wouldnt be desirable

If it were a Control/CompactLogix it would be fairly simple to do. Like you mentioned, I'd generate 50 and store in an array then I could loop through them and ensure each was unique. Unforunately I'm using a Micrologix in this case.
 
I have just grabbed the free running clock on an event (like a user PB) and scale it to my resolution/range. Works pretty good for most stuff.

I have also used excel/DDE/RSLinx Gateway to generate random numbers and fill up a file with them for a training exercise in class.

I have used the MSec from the free running clock and it actually works great. While not actually random it would be very difficult to predict the number as it is generated on an event(money being paid)

But the number can only be between 0-1000???. Perhaps I could multiply this by 10000.

Hmmm...That could work,

So largest number would be 999*10000=999000

Nope that wont work if the Msec were 1 then I'd only have a 5 digit number.

Then secondly I cant ensure the numbers will be unique
 
If the PLC is any Micrologix except the Micrologix 1000 then create an L file with 50 elements in it.

If the PLC is a Micrologix 1000 then you may have little choice except to upgrade or reconsider using a six digit code because even with a random number generator you're not going to get a six digit number.
 
Take a look at the attached example.

Here is a handy shortcut for importing lots of numbers:

Use Excel to generate the random numbers using the RANDBETWEEN(100000,999999) function. Fill five columns by ten rows. Highlight and copy. Now open Notepad and paste the numbers there for temporary keeping.

Open your PLC program and create file L9 with 50 elements. Then save as a .SLC library file. Open the .SLC file using Wordpad. Find where it says DATA L9:0. Right below that paste the ten rows of five numbers from Notepad. Save the .SLC file and close Wordpad. Now open the .SLC file in RSLogix.

I've included the excel file and .SLC file in the attached zip along with a RSLogix500 program so you can see what I did. You can use this method to import up to as many as 256 values per file in as many files as you want.
 
This excel solution would be fine If I had to populate the data table with a series of random numbers just once. The problem is I'll need 50 of these everyday across about 40 machines. It wouldnt be ideal manually creating these in Excel everyday and having to attend each site to fill the data table.

I need a resonably clever way to generate a random number on an event. Loop through the ones I have already generated and ensure it is unique.

How about this. Using the RTC as a seed generate a random number 50 times and place in an L file. Loop through them and ensure none are the same, if they are repeat and check. If not use the first one on demand and increment pointer to next one. When pointer hits 49, repeat.

Seems like horrible inefficient code, be nice I could generate on demand, check against ones currently in use.

Perhaps someones with better mathematics can assist here. If I generate a random number using RTC as a seed. All the remaining 49 will be generated from the first. Is it possible to obtain the same number twice?
 
You can update the table by any number of means. Since the trigger event is likely to be random then something like this would work for updating each value with a random result.

L9:55 := (S:4*65536)+S:4

For each x L9:0 through L9:49 L9:x := L9:x XOR L9:55.
 
You can update the table by any number of means. Since the trigger event is likely to be random then something like this would work for updating each value with a random result.

L9:55 := (S:4*65536)+S:4

For each x L9:0 through L9:49 L9:x := L9:x XOR L9:55.

Alaric,

Thanks for your help. I will try this tomorrow. This will give me 50 unique random numbers?

That wasn't so bad. Thanks for your help
 
I have used the MSec from the free running clock and it actually works great. While not actually random it would be very difficult to predict the number as it is generated on an event(money being paid)

But the number can only be between 0-1000???. Perhaps I could multiply this by 10000.

Use the number of bits required and scale to a float, then move to an INT.

You need 10 random bits, so just map the faster moving bits to the more significant place in your return value and map the remaining bits somewhat randomly, then scale to a float and mov to an int.

EDIT: For an entry code, definitely do the above and generate them on demand (when the customer receipt is printed)...to me ten rungs of XIC OTE is elegant...

The timing between humans is going to be plenty random...

Also, you want a random number between 0000 and 9999, or just 000 to 999?


The rest of this post is moot.

If you do it right, you can change both ends of your range at runtime (to get -315 to +480 for example...).

Now trying to get fifty consecutive random numbers from a clock...
:)

You need to reseed the function clock each time, and that is where an external event is useful...can you afford two or twenty scans of PLC logic between generating these numbers? If you can spend a few seconds generating them, condition the function call with a fast changing ever present MSG DN bit or something...

If the machine is networked, and I needed 50 random numbers in "right now" fashion, I would use excel and DDE/OPC to poke them in.

Now I guess I should read the rest of the thread... and see what the real gurus came up with that's more elegant.
 
Last edited:
Only get back to this now.

Not sure I understand you when you say use the number of bits required and scale to a float?

Do you mean Max bits to represent 999999?

How do I ensure that the min number is never below 100000?

I tried to use the logic L9:xx = (S:4 *65536)+ S:4 with no success. This dosent give a 6 digit number, it gives numbers up to 11 digits and swings both positive and negative
 

Similar Topics

I got my PanelView Plus 7 working with a Micrologix 1500. How would I connect my laptop to the PanelView to view the ladder logic while operating...
Replies
6
Views
161
Hello, I am trying to replicate a piece of logic on the PLC5 onto an SEL RTAC. I am using ladder on SEL and FBD. I am having issue on the ladder...
Replies
13
Views
244
Hello again..trying something on an existing poorly written program and just wanted to double check something system is an A-B MicroLogix 1200 In...
Replies
5
Views
175
Good morning fellow sea captains and wizards, I am being asked to do the above and obtain 4 values from each slave, I know about the MRX and MWX...
Replies
32
Views
849
I have a machine which is undergoing upgradation. As part of the process two SEW drives are being replaced., existing Gen B with new Gen C. The...
Replies
3
Views
204
Back
Top Bottom