0

In my application, i am using datepicker from jqueryui, It needs the unique id's for each input element. to do that,

i use this function (example):

   var x = 100;

    $("a").on("click", function(){
        console.log(Math.floor(Math.random() * x ));
    })

my question is, how to i assure, that no random number will not get repeated. so, i can avoid the duplicate id's.

Thanks in advance..

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

4 Answers4

1

Using random to set custom ID is not a good idea. Why not just make an array and increment ID for each element ?

FLX
  • 2,626
  • 4
  • 26
  • 57
  • Is it answer or comment ? I think this should be in comment section because its not an answer of problem described. – rahularyansharma Jun 04 '13 at 09:07
  • 1
    You're right, but shouldn't I give a way for a better solution instead of give the answer to a bad question ? – FLX Jun 04 '13 at 09:11
  • I agree with FC, using random numbers for variable/control ids is a bad idea. And then having to check whether the value has been used again each time you add a new one? Ridiculous... – Ted Jun 04 '13 at 09:13
  • 1
    Even if you're cloning, answer is the same. Just increment the ID counter and give the result to your clone ! – FLX Jun 04 '13 at 09:18
1

Don't use random numbers, use a counter instead.

var x = 100;

$("a").on("click", function(){
  x++;
  console.log("id" + x);
});
Ted
  • 3,985
  • 1
  • 20
  • 33
1
var x = 100,
    usedNumbers = [];

$("a").on("click", function(){
    var number = Math.floor(Math.random() * x );

    if ($.inArray(number, usedNumbers)) {
        number = Math.floor(Math.random() * x );
    }
    else {
        usedNumbers.push(number);
    }

    console.log(number);
    console.log(usedNumbers);
});

It could happen that you do get an already taken number with this, so if necessary, you should create a loop which only finishes as soon as a new, untaken number was created

Alex
  • 9,911
  • 5
  • 33
  • 52
  • Using random numbers for variable/control ids is a bad idea. And then having to check whether the value has been used again each time you add a new one? Isn't that unnecessarily taxing on the performance??? – Ted Jun 04 '13 at 09:15
  • Wow, random control IDs getting chosen as an answer... This website is going down the drain... – Ted Jun 04 '13 at 12:42
1
//Object capable of generating random ids through recursion triggered by filtering result

    var uniqueRandom = {
        randoms: [],
        getRandom: function(){
            var random = Math.floor(Math.random() * 10); //Make this your size, I used 10 for easy testing
            if(this.randoms.filter(function(elem){ return elem == random}).length > 0){
               return this.getRandom();
            }else{
               this.randoms.push(random);
               return random;
            }
        }
    }

//Usage
    for(var i = 0; i < 10; i++){
       console.log(uniqueRandom.getRandom());
    }

Working Example http://jsfiddle.net/q6SRs/1/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189