View Full Version : serial number generator
rnd me
01-05-2011, 08:31 PM
alert(
(9e15 * Math.random()).toString(36).slice(0,8)
);
jmrker
01-16-2011, 04:59 AM
Probably works for almost all cases,
but still has a small chance of duplications, does it not? :confused:
Krupski
01-16-2011, 07:03 AM
alert(
(9e15 * Math.random()).toString(36).slice(0,8)
);
I use a similar algorithm to make a unique ID for a temporary <span> element that gets deleted milliseconds later.
My algorithm uses the current TIME, so the string it generates will NEVER be duplicated.
Here is is:
var uid = new Date().getTime().toString(36).toLowerCase();
"uid" is a string of random lowercase numbers and letters which typically look like this: gizlmq8j (always 8 characters).
(edit to add): Thinking about it... there IS a possibility that if my code is run twice at the exact proper times, it could return the same string because one of the characters might be uppercase and one lowercase (for example, gizlmq8j and gizlMq8j which (after the .toLowerCase()) would be the same.
Maybe remove the .toLowerCase() part.... for what I use the algorithm for, it makes no difference. If you really need the string to NEVER repeat, remove the lower case part.
jmrker
01-17-2011, 05:10 AM
I use a similar algorithm to make a unique ID for a temporary <span> element that gets deleted milliseconds later.
Usage like that makes some sense to me
(edit to add): Thinking about it... there IS a possibility that if my code is run twice at the exact proper times, it could return the same string because one of the characters might be uppercase and one lowercase (for example, gizlmq8j and gizlMq8j which (after the .toLowerCase()) would be the same.
Maybe remove the .toLowerCase() part.... for what I use the algorithm for, it makes no difference. If you really need the string to NEVER repeat, remove the lower case part.
Seems to me that if the element is deleted milliseconds later
that unless the computer is super fast, should never happen in a 24 hour period.
siberia-man
01-17-2011, 05:46 AM
rnd me
One question for you. Why do you use the factor 9e15? Is this some kind of magic number?
Beow is another vision of serial number generator. Windows-only way!
function GeneratePassword(L)
{
return new ActiveXObject('Scriptlet.TypeLib').Guid.replace(/[^\w]+/g,"").slice(0,L);
}
But Krupski's way without toLowerCase() method is better. In my opinion...
rnd me
01-17-2011, 09:07 AM
rnd me
One question for you. Why do you use the factor 9e15? Is this some kind of magic number?
i didn't use toLowerCase() because it reduces variety.
mine's just a quick way to get a one in 3trillion id. it's got a lot of uses and is one line of code. it's great for instances, pretty good for long-term storage.
if you check for prior existence, anything can be great, but if your system can easy produce the same values on a different computer, it might have trouble imagining un-taken keys...
siberia-man
e9e == 9,000,000,000,000,000 .
jmarker: the chance of a repeat is 1 in 2,821,109,907,456 .
I think it's pretty safe in practice. you can concat two IDs from my algo if you feel unsure; that results in 24 zeros when displaying the odds...
Krupski:
basing the id from a timestamp mean you can only assign one id per millisecond. This makes it impossible to use when populating sets of elements or objects.
it also makes it very easy for someone in you work group to hog a group of IDs just before you try to use them, resulting in conflicts or delays.
there also another flaw you're likely not considering: .getTime() is based upon your local time, not UTC time.
if your computer sets its clock automatically for daylight savings time, that means one day you'll be assigning the same IDs two hours in a row...
siberia-man
01-18-2011, 12:38 PM
basing the id from a timestamp mean you can only assign one id per millisecond. This makes it impossible to use when populating sets of elements or objects.
it also makes it very easy for someone in you work group to hog a group of IDs just before you try to use them, resulting in conflicts or delays.
there also another flaw you're likely not considering: .getTime() is based upon your local time, not UTC time.
if your computer sets its clock automatically for daylight savings time, that means one day you'll be assigning the same IDs two hours in a row...
Really, You're right.
e9e == 9,000,000,000,000,000 .
Sorry, but I did not understand you. I know what is 9e15, but I have interested why this number is used exactly, not others. Could you be so kind as to clarify this point, please. What is e9e? -- misprint?
rnd me
01-18-2011, 11:38 PM
I know what is 9e15, but I have interested why this number is used exactly, not others. Could you be so kind as to clarify this point, please. What is e9e? -- misprint?
9e9 is my goto "big number", i must have typed it without thinking instead of 9e15..
well, originally i uses 9e15 to generate large sets of digits. Going any higher results in exponential notation, which is less-suitable for using as a key.
However, i forgot that a base 36 number doesn't need exponential notation.
so, a "better" version can be produced:
(9e35 * Math.random()).toString(36).slice(0,23);
that should eliminate any concern about accidental repeats; the odds are 1 / 900,000,000,000,000,000,000,000,000,000,000,000 !
jmrker
01-19-2011, 05:10 PM
...
that should eliminate any concern about accidental repeats; the odds are 1 / 900,000,000,000,000,000,000,000,000,000,000,000 !
That's quite acceptable to me. :eek::thumbsup::):D;)
Krupski
01-19-2011, 09:25 PM
Seems to me that if the element is deleted milliseconds later
that unless the computer is super fast, should never happen in a 24 hour period.
"getTime()" returns the number of milliseconds since 1 January 1970. Unless one's computer is fast enough to go back in time, my random number will *never* repeat.
Krupski
01-19-2011, 09:28 PM
Krupski:
basing the id from a timestamp mean you can only assign one id per millisecond. This makes it impossible to use when populating sets of elements or objects.
Quite true if you use it multiple times at once... then this is the answer:
function getUid()
{
var uid = 0;
while ((uid = new Date().getTime()) == new Date().getTime()) { /* insure never a dupe */ }
return uid.toString(36);
}
Krupski
01-19-2011, 09:35 PM
But Krupski's way without toLowerCase() method is better. In my opinion...
The "toLowerCase" means nothing and, indeed is not necessary.
The "toString(36)" function returns it's input modulo 36.
Notice that 36 is 10 + 26 (that is, 0 thru 9 and a thru z). It never returns uppercase letters. ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.