Reputation: 3015
I use the following code for generating the random number from 0 to 15. I use a function random() for generating the unique number i call the function like this
cat=random();
I save the random number with in the array r[]. and check the newly generating number is in the array or not. If the duplicate occurs i call the random() function once again. I use alert for just check it correctly working or not
function random(){
var ran,max=0,min=0;
max=r.length;
alert(max);
if (max>15)
alert("no more space");
ran=Math.floor(Math.random() * 15) + 0;
for (i=0;i<max;i++)
if (ran==r[i])
min++;
if (min>0){
alert("calling");
random(); //return in here
}else{
i=parseInt(max);
r[i]=ran;
return(ran);
alert(ran);
}
}
But the variable return within the function when the duplication occurs can anybody help with this.
Upvotes: 0
Views: 6050
Reputation: 11
i modified a solution that was useful fr me it gets rid of empty entries between numbers and fills them with unique number between 1-9
var arr = [,2,,4,,6,7,,]; //**example**<br/>
while(arr.length < 9){<br/>
var randomnumber=Math.floor(Math.random()*9+1);<br/>
var found=false;<br/>
for(var i=0;i<arr.length;i++){<br/>
if(arr[i]==randomnumber){found=true;break;}<br/>
}<br/>
if(!found)<br/>
for(k=0;k<9;k++)<br/>
{if(!arr[k]) //**if it's empty !!MODIFICATION**<br/>
{arr[k]=randomnumber; break;}}<br/>
}<br/>
alert(arr); //**outputs on the screen**<br/>
Upvotes: 1
Reputation: 2290
Bored, quick hack job, but I believe it'll work:
// Minimum random number
var min = 0;
// Maximum random number
var max = 15;
// Returns a random number between min and max
function random() {
var random_number = Math.random();
return Math.floor((random_number * max) + min);
}
// Returns false if number is in the array
function random_is_unique(random_num_, array_) {
// Could use indexOf, but just looping here for simplicity.
// And not so sure IE has this capability.
for(i = 0; i < array_.length; i++) {
if(array_[i] == random_num_) {
return false;
}
}
return true;
}
// Returns a guaranteed unique, or -1 if no more unique values
// are availble to return
function guaranteed_unique(array_) {
random_number = random();
// Just an iterator, so we have a terminating condition
tries = 0;
while(!random_is_unique(random_number, array_)) {
// This is dumb. There's likely a better way to do this, but it's
// quick and dirty. It also does not guarantee you've tested all
// integers.
if(tries > max) {
return -1;
}
random_number = random();
tries++;
}
return random_number;
}
my_array = new Array();
my_array[0] = 1;
my_array[1] = 15;
my_array[2] = 6;
my_array[3] = 9;
my_array[4] = 13;
my_random_number = guaranteed_unique(my_array);
alert("Random number is " + my_random_number);
Upvotes: 1
Reputation: 324567
I'd create an array and shuffle it using Fisher-Yates.
function shuffle(arr) {
var shuffled = arr.slice(0), i = arr.length, temp, index;
while (i--) {
index = Math.floor(i * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled;
}
// Create the array
var i = 16, arr = [];
while (i--) arr[i] = i;
// Shuffle it
arr = shuffle(arr);
// Array is now the numbers 0-15 in a random order
console.log(arr);
Upvotes: 5