2016-02-13 50 views
0

我只想生成的隨機數將不會重複,每當我嘗試把數字,生成的隨機數有重複,任何其他的想法。我應該在這裏改變什麼?唯一生成的隨機數(不重複)

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; 
+0

爲什麼你循環while(zRandomString!= z)'?當zRandomString最終要等於'z'時,它是隨機的。它沒有任何意義。 – trincot

+0

你是否想做類似這樣的事情:http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript?計算結果爲 –

+0

,將是唯一且不重複的。上面的小提琴,可以請幫忙嗎?任何想法我應該在這裏使用|? – Zipp

回答

0

嘗試使用此方案:

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

它會返回一個隨機數,從0到9,但是6. – begoyan

+0

解決Asker問題以外的問題。 –

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

另一個我的ThOD是先shoffle池,然後彈出,元素:

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功能非常不同。當然,最終你會耗盡游泳池。然後您可以決定重新創建或重新排列陣列。還要注意,如果池中有重複的條目,這些方法將產生重複的元素。

+0

先生,因爲我有計算,我dnt知道爲什麼我的小提琴ont工作,無論如何,結果將不會創建一個重複的隨機數 – Zipp

+0

http://postimg.org/image/8zjlg2rsp/這是我的PS,所以,就像你可以看到,生成的隨機數的計算是8,我希望它只是不重複.. 8號是refres a,b,c,d,e,f,g,h /// a + b +那麼W(13)/// e + f + g就是X(52)///那麼d = Y(35)///和a + b + c + e + d + f + g + h = Z(101) – Zipp

+1

我真的不明白你的意思,我害怕。 –