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

Unique in generated random number (no duplicate)

本文关键字:唯一 随机数      更新时间:2023-09-26

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

看小提琴

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;

尝试使用此方案:

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

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

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函数,这两种方法并没有太大区别。当然,最终你会耗尽游泳池。然后,您可以决定重新创建或重新洗牌阵列。另请注意,如果池具有重复条目,则这些方法将生成重复的元素。