0

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

cat=random();

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

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

但是当重复发生时,函数内的变量返回可以帮助解决这个问题。

4

3 回答 3

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);
于 2012-07-06T20:14:52.737 回答
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);
于 2012-07-06T20:24:39.147 回答
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/>
于 2012-11-03T20:42:08.430 回答