|
|
Create Unique Random number in javascript - Javascript
|
Views : 1214
|
|
Tagged in : Javascript
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
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;
}
|
|
By Vinoth, On - 2010-02-01 |
|
|
|