Expand my Community achievements bar.

Unique Auto generated number

Avatar

Former Community Member
Hello



I did a search and found this topic,
digitaldave2010, "Generating a Random Number for application Form" #2, 12 Dec 2005 11:17 am



It helped a lot, but I need a little more help. When someone opens the form, I want to create a random number. Once the number is created, I want it to stay with that form, so I can't use the form open event. I am using Math.random but there is still a chance that two numbers can be the same.



Is there a way to take the current date and time and convert it to a random number that will always be unique or is there an algorithm that will generate a unique number with a very low chance of ever being a duplicate?

Also how can I get the form to not generate a unique number once one has been generated.



Thanks

Kevin
5 Replies

Avatar

Former Community Member
As far as I know, there is no way to generate a number, which is random AND globally unique. This way, it would no longer be random. :-)



Chances that two random 6-digit numbers are the same are pretty low indeed.

Let's suppose, you create 1,000 documents. Then the chances you get a duplicate number are 0.1 percent. Am I right? I'm quite puzzled...

Anyway, you may want to use 9-digit random numbers to further diminish that figure.



Do you want to store that number with the current document? I suppose, you can generate a hidden field, where the variable gets stored. But I'm not sure.



Regards,

Steve

Avatar

Former Community Member
Using a 6+ digit number will have low chances in the beginning, but we fill out around a hundred of these forms a day so if you look farther down the road, your chances of getting a duplicate increase. I did make a number generator based on concating the current date/time to the millisecond. This will keep the chance very low throughout the future.



Since we have multiple people filling out this form in different locations; the only way to get a duplicate would be if someone clicked on the specifed field at the exact millisecond which is nearly impossible.



Here is the code that I use for anyone else facing the same problem:



if(F.P1.AUTO_NUM.rawValue == null)

{

var d = new Date();

var code = d.getDay().toString() + d.getMonth().toString() + d.getFullYear().toString() + d.getHours().toString() + d.getMinutes().toString() + d.getSeconds().toString() + d.getMilliseconds().toString();



//Set to code the the text field

F.P1.AUTO_NUM.rawValue = code;

}



The number is sorta long but can be cut down by taking out the milliseconds and going to the second. Still a low chance.

Avatar

Former Community Member
Hello Kevin,



I would suggest you Google UUID - Universally Unique ID. UUIDs are 128-bit numbers formed using the current time plus the "node ID" of the computer in some algorithm and I read once that you would have to generate a UUID every second for a few hundred years before you'd likely get a collision. That sort of risk seems to be generally accepted throughout the programming community to the extent that components such ActiveX controls and Applets are uniquely identified in this way.



I think there is a library in Java to do this for you, you may find web resources for JavaScript solutions.



Neil

Avatar

Former Community Member
How about actually storing the ID somewhere in a central database, whenever you get an ID, do a query to the database to see if it exists, if it does, regenerate an ID.



Wee

Avatar

Former Community Member
Because, at a hundred forms a day the database table will get very large very quickly and the query will take an extraordinary time to execute.



It would be more efficient, I think, to generate a UUID and attempt the insert into the database. If the field in the database accepting the UUID is the primary key or, at least, marked as UNIQUE, then an exception would be thrown if there is a collision. In which case another UUID could be generate and the insert tried again. The chances of a collision in the first insert, using a UUID, is very low anyway so the insert will usually be successful. The chances of a subsequent collision are even lower.



Neil