0

I want only the generated random number will hava no duplicate, whenever i try to put the number, the generated random number has duplicate, any other idea. what should i change here?

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;
tutuDajuju
  • 10,307
  • 6
  • 65
  • 88
Zipp
  • 3
  • 4
  • Why do you loop `while( zRandomString != z)` ? It makes no sense for `zRandomString` to be random when in the end you want it to be equal to `z`. It does not make sense. – trincot Feb 13 '16 at 13:41
  • Are you trying t do something like: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript? – Jeremy J Starcher Feb 13 '16 at 13:41
  • the result of the computation, will be unique and no duplicate. the fiddle above, can please help? any idea what should i use here|? – Zipp Feb 13 '16 at 13:47
  • It is not clear what you want exactly, not even with the fiddle, which doesn't seem to work for me. Do you want to create up to ten random numbers without repetition? – M Oehm Feb 13 '16 at 17:22
  • https://jsfiddle.net/#&togetherjs=3slqwd2ryd – Zipp Feb 13 '16 at 17:37

2 Answers2

0

Try to use this scheme:

var r;
do {
    r = Math.floor(Math.random() * 10);
} while (r == 6);
begoyan
  • 404
  • 5
  • 10
0

It is not quite clear what you want, but one way to create a sequence on unique random numbers is to create a pool of numbers and sucessively pick and remove items from it:

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

Another method is to shoffle the pool first and then pop off elements:

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

These two methods aren't very different if you look at the pick and shuffle functions. Of course, eventually you will exhaust the pool. You can then decide to recreate or reshuffle the array. Also note that these methods will produce repeated elements if the pool has repeated entries.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • sir because i have computation, i dnt know why my fiddle ont working, anyway., the result will not create a duplicate random number – Zipp Feb 13 '16 at 17:46
  • http://postimg.org/image/8zjlg2rsp/ this is my PS, so, as you can see, the generated random number with computation is 8, and i want it only not to duplicate.. the 8 numbers is refres to a,b,c,d,e,f,g,h /// a+b+c thats the W (13) /// e+f+g is X (52) /// then d = Y (35)/// and a+b+c+e+d+f+g+h = Z (101) – Zipp Feb 13 '16 at 17:54
  • 1
    I really don't understand what you mean, I'm afraid. – M Oehm Feb 13 '16 at 18:05