Webmaster.Gotactics
Webmaster.Gotactics

Reputation: 351

How to generate an random 8 character string that won't occur twice in jQuery?

How do I generate a random string that is only 8 characters long, that will not occur twice in jQuery.

Upvotes: 0

Views: 1416

Answers (4)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

If you mean won't occur twice anywhere (meaning, on other people's computers, or in different browsers on the client's computer) then the short answer is, in pure javascript that's simply not possible, since that relies on getting a user's network SSID to use as a key, and javascript doesn't have access to that.

If you mean a random number that won't repeat on the user's client over some period of days (or until they clear their cookies), then grab a random number, take 8 digits from it and put it in a cookie. Then test against that cookie when you get the next number (and store that as well). That guarantees a unique number for that user, in that browser.

If you mean won't repeat simply in a given session, then jfriend's answer is your best bet.

Upvotes: 0

Jason
Jason

Reputation: 3485

I think this is exactly what you're asking for. At any time in your code, you can ask for a random, 8-character string via rString.get():

   var rString = {
       get : function() {
           var _this = this,
               randomString,
               charset = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-=_+".split('');

           var generate = function(len) {
               var output = '';
               for(var i=0; i<len; i++) {
                   output += charset[ Math.floor(Math.random()*charset.length) ];
               }
               return output;
           }

           var inArray = function(arr, item) {
               for(var i=0; i<arr.length; i++) {
                   if(arr[i] === item) {
                       return true;
                   }
               }
               return false;
           }

           while( true ) {
               if( !inArray( this.previous, randomString = generate(8) ) ) {
                   this.previous.push(randomString);
                   return randomString;
               }
           }

       },
       previous : []
   };

This fiddle will generate 10 random items for you.

Upvotes: 0

jfriend00
jfriend00

Reputation: 707318

This will generate the desired range of random numbers and keep a history of previously generated numbers so it will never generate the same number twice:

function generateRandom() {
    if (!generateRandom.prevNums) {
        generateRandom.prevNums = {};
    }
    var random;
    do {
        random = Math.floor((Math.random() * (99999999 - 10000000 + 1)) + 10000000);
    } while (generateRandom.prevNums[random])
    generateRandom.prevNums[random] = true;
    return(random.toString());
}

Working demonstration here: http://jsfiddle.net/jfriend00/ZfKmk/

Upvotes: 0

Roy Tang
Roy Tang

Reputation: 5761

Why not just use the current time (as an integer)?

Upvotes: 2

Related Questions