使用javascript生成唯一的随机数

时间:2012-07-06 20:07:23

标签: javascript jquery html5

我使用以下代码生成0到15之间的随机数。我使用函数random()生成唯一的数字我调用这个函数

cat=random();

我将随机数保存在数组r []中。并检查新生成的数字是否在数组中。如果发生重复,我再次调用random()函数。我使用警报只是检查它是否正常工作

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

但是复制发生时函数内的变量返回可以让任何人帮忙解决这个问题。

3 个答案:

答案 0 :(得分:5)

我创建了一个数组并使用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);

答案 1 :(得分:1)

无聊,快速的黑客工作,但我相信它会起作用:

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

答案 2 :(得分:1)

我修改了一个对我有用的解决方案 它消除了数字之间的空条目,并用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/>