3

我正在尝试制作一款有趣的宾果游戏。我在很多地方都找了一个独特的发电机,但我似乎找不到。我试着自己做,但是一旦它实际上达到了一个相同的数字,它就会无限循环。我尝试了一个简单的代码,理论上应该可以工作,但由于某种原因,事情通过了。我能做些什么!?

var bc = [];
for (var i = 0; i < 5; i++) {
  var r = Math.floor(Math.random()*20+1) + 0;
  if(!(r in bc)){
    bc.push(r);     
    }
    else
    {
    i--;
    }
}
____________________________________________
____________________________________________
____________________________________________
b1=0;
b2=0;
b3=0;
b4=0;
b5=0;
var bc = [b1,b2,b3,b4,b5]
var bnc = function(){
    var n = Math.floor(Math.random() * 5+1)+0;
    var n2 = Math.floor(Math.random() * 5+1)+0;
    b1 = n;
    var a1 = true;
    var as = false;
    while(a1){
        var c = n;
        if(c===b1||c===0 ||as!==false){
        c = n2;
        as=true;
        }
        if(c===b1||c===0&&as===true){
        c = n;
        as=false;
        }
            if(c!=b1){
            b2 = c;
            a1 = false;
            a2 = true;
        }
    }
};
bnc();
console.log("new1");
console.log(b1,b2,b3,b4,b5);
//_______________________________________
var bnc2 = function(){
    var n = Math.floor(Math.random() * 5+1)+0;
    var n2 = Math.floor(Math.random() * 5+1)+0;
    var a1 = true;
    var as = false;
    while(a1){
        var c = n;
        if(c===b1||c===b2||c===0&&as===false){
        c = n2;
        as=true;
        }
        if(c===b1||c===b2||c===0&&as===true){
        c = n;
        as=false;
        }
        if(c!=b1&&c!=b2){
            b3 = c;
            console.log("made it 1");
            a1 = false;
        }
    }
};
bnc2();
console.log("new2");
console.log(b1,b2,b3,b4,b5);
4

4 回答 4

8

一旦它实际达到一个相同的数字

它永远不应该。此类算法运行时间越长,运行时间越长。你应该采取不同的方法:

将所有可能的数字放入一个池中。抽到号码后,将其从池中取出。就像在现实生活中所做的一样。

var pool = [1, 2, 3, 4, 5];
var getNumber = function () {
    if (pool.length == 0) {
        throw "No numbers left";
    }
    var index = Math.floor(pool.length * Math.random());
    var drawn = pool.splice(index, 1);
    return drawn[0];
};
于 2013-03-23T08:49:11.590 回答
3

我宁愿这样做:

http://jsfiddle.net/YC58s/

generate = function(length)
{
    var arr = [];
    var n;
    for(var i=0; i<length; i++)
    {
        do
            n = Math.floor(Math.random()*20+1);
        while(arr.indexOf(n) !== -1)

       arr[i] = n;
    }

    return arr;
}
于 2013-03-23T08:37:25.417 回答
0

This can handle generating upto 20 digit UNIQUE random number

JS

var generatedNumbers = [];

function generateRandomNumber(precision) { // precision --> number precision in integer 
    if (precision <= 20) {
        var randomNum = Math.round(Math.random().toFixed(precision) * Math.pow(10, precision));
        if (generatedNumbers.indexOf(randomNum) > -1) {
            if (generatedNumbers.length == Math.pow(10, precision))
                return "Generated all values with this precision";
                return generateRandomNumber(precision);
        } else {
            generatedNumbers.push(randomNum);
            return randomNum;
        }
    } else
       return "Number Precision shoould not exceed 20";
}
generateRandomNumber(1);

enter image description here

JsFiddle

于 2016-04-27T13:49:13.903 回答
-1
window.onload = unRanNumGen(20, 1, 12);
    //above, we need 12 random numbers between 20 (included) and 1(included)

function unRanNumGen(max, min, limit){

    //max = maximum number [inclued] (range of numbers)
    //min = minimum number [included] (range of numbers)
    //limit = number of random numbers (how many numbers do you want?)

    var pool = [genRan(max, min)];

    for(i=0; i<limit; i++){
        for(n = 0; n <i; n++){

            if(pool[n] !== genRan(max, min)){
            pool.push(genRan(max, min));

            break;
            }
        }

    }

function genRan(max, min){
    var genRan = Math.floor(Math.random() * (max - min) + min);
    return genRan;
}

alert(pool.join('\n')); //to display the array of random numbers

}
于 2014-07-27T03:48:51.060 回答