To make sure the random numbers are truly unique, you have to keep track of all random numbers that came before and if you get a conflict, generate a new one:
var makeUniqueRandom = (function() {
var randoms = {};
return function(low, hi) {
var rand;
while (true) {
rand = Math.floor((Math.random() * (hi - low)) + low);
if (!randoms[rand]) {
randoms[rand] = true;
return rand;
}
}
}
})();
Usage:
var rand = makeUniqueRandom(1, 10);
Notes: You have to make absolutely sure you don't exhaust the available random numbers because if you do, it will turn into an infinite loop. So, if you're asking for random numbers from 1-10 (9 possible values), then don't call it more than 9 times.
To put random values into <input>
tags, you can do this:
<input type="text" class="pcount pc1" value="10"/>
<input type="text" class="pcount pc2" value="13"/>
<input type="text" class="pcount pc3" value="16"/>
$(".pcount").each(function() {
this.value = makeUniqueRandom(1, 10);
});