Create Unique Random number in javascript

by Vinoth 2010-02-01 11:25:48

Here I write a code to create unique random numbers. When you run this code numbers array will contain unique random numbers

Here
/*
list: javascript array;
max: maximum limit for a random number
*/
function generate_unique_ran(){
numbers=new Array('0');
var array_length=10
for(j=0;j new_numbers = unique_rand(numbers,81); //Give random position with a maximum limit of 81
numbers.push(new_numbers);
}
}
function unique_rand(list,max) {
var number = (Math.random()*parseInt(max)).toFixed(0);;//Get a random number from 0 to max
var a=0;
for(a=0;a<list.length;a++) {
if(list[a] == number) { //If number already Present
number = (Math.random()*parseInt(max)).toFixed(0);a=-1; //Get a new Number and start it again

}
}
return number;
}


2787
like
0
dislike
0
mail
flag

You must LOGIN to add comments