Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
function randomFromInterval(from, to, qty) {
var arr = [];
for (var i=0; i <= qty; i++) {
arr[i] = Math.floor(Math.random() * (to - from + 1) + from);
}
return arr;
}
Array.prototype.inArray = function(v) {
for(var i=0;i<this.length;i++) if(this[i] === v) return true;
return false;
}
function randomFromInterval(from, to, qty) {
var arr = [];
while(arr.length<=qty) {
var r = Math.floor(Math.random() * (to - from + 1) + from);
if(!arr.inArray(r)) arr.push(r);
}
return arr;
}
function randomFromInterval(from, to, qty) {
var r,arr = [];
while(arr.length<=qty) {
r = Math.floor(Math.random() * (to - from + 1) + from);
if(!arr.indexOf(r)) arr.push(r);
}
return arr;
}
if( (arr.indexOf(r)) === -1 ){
arr.push(r);
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
This one seems quite elegant
http://stackoverflow.com/a/2380349/295783