0

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.

8
  • You've got some inadvertent recursion going on there. Why are you calling random() from inside random? Commented Jul 6, 2012 at 20:13
  • Also, you have an alert() after return. This will never be called. Commented Jul 6, 2012 at 20:14
  • Is the desired affect to have random return an array of 15 random numbers that are between 0 and 15? Commented Jul 6, 2012 at 20:14
  • @Lee: To avoid duplication of the generating number Commented Jul 6, 2012 at 20:16
  • @Grinn: Cant get u.. Can you explain Commented Jul 6, 2012 at 20:17

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

There is a shorter way, using a second array: github.com/bfontaine/Boudou/blob/master/src/boudou_dev.js#L840
@boudou: That's not shorter once you include the implementation of Array.prototype.pop_index. It's also less efficient for larger arrays to use splice().
I had a really really bad and wild attempt at doing this I was piecing together over the past day or so, and came across this and have to say, its brilliant in its simplicity and efficiency. bravo, sir, bravo.
1

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);

Comments

1

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/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.