Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts

    serial number generator

    Code:
     alert(
       (9e15 * Math.random()).toString(36).slice(0,8)
    );
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,097
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    Probably works for almost all cases,
    but still has a small chance of duplications, does it not?

  • #3
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rnd me View Post
    Code:
     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:

    Code:
        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.
    Last edited by Krupski; 01-16-2011 at 07:07 AM.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,097
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Arrow

    Quote Originally Posted by Krupski View Post
    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

    Quote Originally Posted by Krupski View Post
    (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.

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    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!
    Code:
    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...

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Quote Originally Posted by siberia-man View Post
    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...
    Last edited by rnd me; 01-17-2011 at 09:19 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #7
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Quote Originally Posted by rnd me View Post
    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?

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Quote Originally Posted by siberia-man View Post
    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:
    Code:
    (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 !
    Last edited by rnd me; 01-18-2011 at 11:46 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,097
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Quote Originally Posted by rnd me View Post
    ...

    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.

  • #10
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by jmrker View Post
    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.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #11
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by rnd me View Post
    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:

    Code:
    function getUid()
    {
        var uid = 0;
        while ((uid = new Date().getTime()) == new Date().getTime()) { /* insure never a dupe */ }
        return uid.toString(36);
    }
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #12
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by siberia-man View Post
    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.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •