生成的随机数唯一(无重复)

时间:2016-02-13 13:31:30

标签: javascript regex algorithm random numbers

我只想生成的随机数将没有重复,每当我尝试输入数字时,生成的随机数都有重复,任何其他想法。我应该在这里改变什么?

See the fiddle

var arr = hello.toString(10).replace(/\D/g, '0').split('').map(Number);
for(i=0;i<arr.length;i++) arr[i] = +arr[i]|0;

//initialize variables 
var z = arr[3]; 
var y = arr[2];
var x = arr[1];
var w = arr[0];

while((((a2 <= 0) || (a2 > 49)) || ((b <= 0) || (b > 49)) || ((c <= 0) || (c > 49)) || ((d <= 0) || (d > 49)) || ((e2 <= 0) || (e2 > 49)) || ((f <= 0) || (f > 49)) || ((g <= 0) || (g > 49)  ))){

    //loop ulit kapag hindi match yung random number sa value nung z
    while( zRandomString != z){     
         zRandom = Math.floor(Math.random() * (109 - 100 + 1)) + 100;
         zRandomRound = zRandom % 10;
         zRandomString = zRandomRound.toString();
    }
    var zNew = zRandom; //new value ng z
    document.getElementById("zebra").innerHTML = "Z = " + zNew + "<br />";// udsadsadsad

    h = zNew;

    while( yRandomString != y){     
         yRandom = Math.floor(Math.random() * (49 - 1 + 1)) + 1;
         yRandomRound = yRandom % 10;
         yRandomString = yRandomRound.toString();
    }
    var yNew = yRandom; //new value ng z
    document.getElementById("yeah").innerHTML = "Y = " + yNew + "<br />";// udsadsadsad

    h = h - yNew;

    while( xRandomString != x){     
         xRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
         xRandomRound = xRandom % 10;
         xRandomString = xRandomRound.toString();
    }
    var xNew = xRandom; //new value ng z
    document.getElementById("ex").innerHTML = "X = " + xNew + "<br />";// udsadsadsad

    h = h - xNew;

    while( wRandomString != w){
        wRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
        wRandomRound = wRandom % 10;
        wRandomString = wRandomRound.toString();
    }
    var wNew = wRandom; //new value ng z
    document.getElementById("weh").innerHTML = "W = " + wNew + "<br />";// udsadsadsad

    //h = Math.abs(h - wNew); // new value of h
    h = h - wNew;           

    a = Math.floor(Math.random() * (wNew - 1 + 1)) + 1;
    a2 = wNew - a;
    b = Math.floor(Math.random() * (a2 - 1 + 1)) + 1;   
    c = a - b;  
    d = yNew;
    e = Math.floor(Math.random() * (xNew - 1 + 1)) + 1;
    e2 = xNew - e;
    f = Math.floor(Math.random() * (e2 - 1 + 1)) + 1;
    g = e - f;      
}
var combo = a2.toString() + ', ' + b.toString() + ', ' + c.toString() + ', ' + d.toString() + ', ' + e2.toString() + ', ' + f.toString() + ', ' + g.toString() + ', ' + h.toString();
document.getElementById("combo").innerHTML = combo;

2 个答案:

答案 0 :(得分:0)

尝试使用此方案:

var r;
do {
    r = Math.floor(Math.random() * 10);
} while (r == 6);

答案 1 :(得分:0)

目前还不是很清楚你想要什么,但是在独特的随机数上创建序列的一种方法是创建一个数字池并从中挑选和删除项目:

function pick(pool) {
    if (pool.length == 0) return undefined;

    // pick an element from the pool
    var k = (Math.random() * pool.length) | 0;
    var res = pool[k];

    // adjust array
    pool[k] = pool[pool.length - 1];
    pool.pop();

    return res;
}

// example    
var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

while (pool.length) {
    console.log(pick(pool));
}

另一种方法是先将游泳池移开,然后弹出元素:

function shuffle(arr) {
    var n = arr.length;

    while (n) {
        // pick element
        var k = (Math.random() * n--) | 0;

        // swap last and picked elements
        var swap = arr[k];
        arr[k] = arr[n];
        arr[n] = swap;
    }
}

var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

shuffle(pool);

while (pool.length) {
    console.log(pool.pop());
}

如果查看pickshuffle函数,这两种方法差别不大。当然,最终你会耗尽游泳池。然后,您可以决定重新创建或重新洗牌。另请注意,如果池中有重复的条目,这些方法将生成重复的元素。