A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Randomizing a bank of numbers

  1. #1
    Odisey Odisey's Avatar
    Join Date
    Apr 2004
    Location
    WV
    Posts
    213

    Randomizing a bank of numbers

    Hello,
    Can someone help me figure this math. I have a bank of numbers. I have:

    Twelve 1's
    Twelve 2's
    Twelve 3's
    Twelve 4's
    Twelve 5's
    Twelve 6's
    Twelve 7's
    Twelve 8's

    I want to randomly generate 3 digit numbers with the bank until the bank is empty. One limitation is none of the three digit numbers can have the same numbr in it twice .... like 223. And another is the same three digit number cannot be repeated, such as 345, 345.

    Thank you,
    Odisey
    Much to learn.... Much to learn....

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    PHP Code:
    Array.prototoype.contains = function(element){
        var 
    count this.length;
        while(
    count){
            if(
    this[--count]==element){
                return 
    true;
            };
        };
        return 
    false;
    };
    function 
    getrandomindex(array){
        return 
    Math.round(Math.random()*(array.length-1));
    };
    function 
    getrandomelement(array){
        return array[
    Math.round(Math.random()*(array.length-1))];
    };

    var 
    bank = [];
    for(var 
    i=1;i<9;i++){
        for(var 
    j=0;j<12;j++){
            
    bank.push(i);
        };
    };
    trace(bank);

    var 
    returns = [];

    function 
    gettrips(){
        if(
    bank.length 3){
            return 
    false;
        };
        var 
    getrandomindex(bank);
        var 
    getrandomindex(bank);
        while(
    bank[a]==bank[b]){
            
    getrandomindex(bank);
        }
        var 
    getrandomindex(bank);
        while(
    bank[b]==bank[c] || bank[a]==bank[c]){
            
    getrandomindex(bank);
        };
        var 
    = [bank[a],bank[b],bank[c]].join("");
        
        if(
    returns.contains(d)){
            return 
    gettrips();
        };
        
        
    bank.splice(a,1);
        
    bank.splice(b,1);
        
    bank.splice(c,1);
        
        
    returns.push(d);
        return 
    d;
            
    };

    this.onMouseDown = function(){
        
    gettrips();
        
    trace(returns);
        
    trace(bank.length);
    }; 

  3. #3
    Member
    Join Date
    May 2008
    Location
    Wellington, NZ
    Posts
    35
    Here is a reusable function I tend to use for random sort with no repetition. This will sort any array in random order, with each item appearing once. What it does is swap a element picked at random with the last element of the array. The 'pickable' set will be reduced by 1, swap, etc.

    arrRnd = randomSort(arr)

    PHP Code:
    /**
      randomSort, no repetition
    */
    function randomSort(itms) {
        var 
    oItem:Object = new Object()
        var 
    lastPos:Number itms.length

        
    for(var 0i<itms.lengthi++){
          
    lastPos itms.length-i-1
          rnd 
    Math.floor(Math.random() * (lastPos))
          
    oItem itms[lastPos]
          
    itms[lastPos] = itms[rnd]
          
    itms[rnd] = oItem;
        }
        return 
    itms;


  4. #4
    Odisey Odisey's Avatar
    Join Date
    Apr 2004
    Location
    WV
    Posts
    213

    Almost

    Quote Originally Posted by moagrius
    PHP Code:
    Array.prototoype.contains = function(element){
        var 
    count this.length;
        while(
    count){
            if(
    this[--count]==element){
                return 
    true;
            };
        };
        return 
    false;
    };
    function 
    getrandomindex(array){
        return 
    Math.round(Math.random()*(array.length-1));
    };
    function 
    getrandomelement(array){
        return array[
    Math.round(Math.random()*(array.length-1))];
    };

    var 
    bank = [];
    for(var 
    i=1;i<9;i++){
        for(var 
    j=0;j<12;j++){
            
    bank.push(i);
        };
    };
    trace(bank);

    var 
    returns = [];

    function 
    gettrips(){
        if(
    bank.length 3){
            return 
    false;
        };
        var 
    getrandomindex(bank);
        var 
    getrandomindex(bank);
        while(
    bank[a]==bank[b]){
            
    getrandomindex(bank);
        }
        var 
    getrandomindex(bank);
        while(
    bank[b]==bank[c] || bank[a]==bank[c]){
            
    getrandomindex(bank);
        };
        var 
    = [bank[a],bank[b],bank[c]].join("");
        
        if(
    returns.contains(d)){
            return 
    gettrips();
        };
        
        
    bank.splice(a,1);
        
    bank.splice(b,1);
        
    bank.splice(c,1);
        
        
    returns.push(d);
        return 
    d;
            
    };

    this.onMouseDown = function(){
        
    gettrips();
        
    trace(returns);
        
    trace(bank.length);
    }; 

    This is good... but ,,, here is the output:

    1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3, 3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,5,5, 5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7, 7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8
    Much to learn.... Much to learn....

  5. #5
    Odisey Odisey's Avatar
    Join Date
    Apr 2004
    Location
    WV
    Posts
    213
    Quote Originally Posted by widged
    Here is a reusable function I tend to use for random sort with no repetition. This will sort any array in random order, with each item appearing once. What it does is swap a element picked at random with the last element of the array. The 'pickable' set will be reduced by 1, swap, etc.

    arrRnd = randomSort(arr)

    PHP Code:
    /**
      randomSort, no repetition
    */
    function randomSort(itms) {
        var 
    oItem:Object = new Object()
        var 
    lastPos:Number itms.length

        
    for(var 0i<itms.lengthi++){
          
    lastPos itms.length-i-1
          rnd 
    Math.floor(Math.random() * (lastPos))
          
    oItem itms[lastPos]
          
    itms[lastPos] = itms[rnd]
          
    itms[rnd] = oItem;
        }
        return 
    itms;


    Maybe I can use this somehow.... Thank you
    Much to learn.... Much to learn....

  6. #6
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    odisey - that's just the output of the bank of numbers. run that, then mouse down several times and you'll see it returning what you asked for.

  7. #7
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    maybe this example will be easier to understand. just open a new FLA, paste in frame 1's actions panel, and test movie.
    PHP Code:
    Array.prototoype.contains = function(element){ 
        var 
    count this.length
        while(
    count){ 
            if(
    this[--count]==element){ 
                return 
    true
            }; 
        }; 
        return 
    false
    }; 
    function 
    getrandomindex(array){ 
        return 
    Math.round(Math.random()*(array.length-1)); 
    }; 

    var 
    bank = []; 
    var 
    returns = [];

    for(var 
    i=1;i<9;i++){ 
        for(var 
    j=0;j<12;j++){ 
            
    bank.push(i); 
        }; 
    }; 

    function 
    gettrips(){ 
        if(
    bank.length 3){ 
            return 
    false
        }; 
        var 
    getrandomindex(bank); 
        var 
    getrandomindex(bank); 
        while(
    bank[a]==bank[b]){ 
            
    getrandomindex(bank); 
        } 
        var 
    getrandomindex(bank); 
        while(
    bank[b]==bank[c] || bank[a]==bank[c]){ 
            
    getrandomindex(bank); 
        }; 
        var 
    = [bank[a],bank[b],bank[c]].join(""); 
         
        if(
    returns.contains(d)){ 
            return 
    gettrips(); 
        }; 
         
        
    bank.splice(a,1); 
        
    bank.splice(b,1); 
        
    bank.splice(c,1); 
         
        return 
    d
             
    }; 

    trace(gettrips());
    trace(gettrips());
    trace(gettrips());
    trace(gettrips());
    trace(gettrips());
    trace(gettrips());
    trace(gettrips());
    trace(gettrips());
    trace(gettrips());
    trace(gettrips()); 
    Last edited by moagrius; 08-01-2008 at 07:26 AM.

  8. #8
    Member
    Join Date
    May 2008
    Location
    Wellington, NZ
    Posts
    35
    Quote Originally Posted by Odisey
    Maybe I can use this somehow....
    If you are after creating a bank of 12 times the values 1-8, before the function, try something like this.

    PHP Code:
    // build an array with 12 times the values 1-8
    var arrVal:Array = new Array()
    for (
    1<= 8i++) {
        for (
    012j++) {
            
    arrVal.pushi)
        }
    }

    // sort the array in random order, no repetition
    arrRnd randomSort(arrVal// use the function given in the previous post

    // optional, a pretty output
    commaSep arrayCombine(arrRnd',')
    trace(commaSep)

    /**
      arrayCombine
    */
    function arrayCombine(arrsep) {
      var 
    str:String ''
      
    for (0arr.lengthi++) {
        
    str += arr[i] + sep
      
    }
      
    str str.substr(0str.length sep.length// removes trailing separator
                                                  // faster than an if in the loop
      
    return str

    Last edited by widged; 08-01-2008 at 07:19 AM.

  9. #9
    Member
    Join Date
    May 2008
    Location
    Wellington, NZ
    Posts
    35
    Note however that it doesn't get you any close from solving your problem of avoiding '223'.

    You can try and add conditions like the one below...

    PHP Code:
    // sort the array in random order, no repetition
    arrRnd randomSort(arrVal// use the function given in the previous post
    var arrTriplet:Array = new Array()
    for (var 
    0arrRnd.lengthi++) {
        
    digit arrRnd[i]
        if(
    arrTriplet[0] != undefined && digit == arrTriplet[0]) { next }
        if(
    arrTriplet[1] != undefined && digit == arrTriplet[1]) { next }
        
    arrTriplet.push(digit)
        if(
    arrTriplet.length == 3) { break }
    }
    triplet arrayCombine(arrTriplet'')
    trace (triplet
    but the script above doesn't check that you don't pick the same number twice (345, 345). With a bank approach, you will come across one problem after another.

    Well, the thing is that you really don't need to start from 12 times 1-8 to get all possible combinations of 3 digits made of numbers from 1-8 ;-). The bank is somewhat irrelevant. Especially if each number occurs with the same probability (12 count). You are better off using permutations.


    PHP Code:
    var arrTriplets:Array = new Array()
    for (
    1<= 8i++) {
        for (
    1<= 8j++) {
            for (
    1<= 8k++) {
                if(
    != && != && != ) {
                    
    arrTriplets.push("" "" +  j  "" +  k)
                }
            }
        }
    }

    arrRnd randomSort(arrTriplets)
    commaSep arrayCombine(arrRnd',')
    trace(commaSep
    (see posts above for functions randomSort and arrayCombine).
    Last edited by widged; 08-01-2008 at 07:36 AM.

  10. #10
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    widged: how is arrayCombine any different than join? just curious...

  11. #11
    Member
    Join Date
    May 2008
    Location
    Wellington, NZ
    Posts
    35
    Quote Originally Posted by moagrius
    widged: how is arrayCombine any different than join? just curious...
    I have been coding with PHP for the last month (before you ask, join also exists in php but as an alias of implode)... I couldn't remember the magic word and I had no book at hand at the time I replied. I ran a rapid search on the web and wasn't successful, so I bundled a function of my own :-)

    Yup, always use native functions when available.
    Last edited by widged; 08-02-2008 at 12:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center