A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Making Math.random() not repeat its self.

  1. #1
    Member
    Join Date
    May 2009
    Posts
    66

    Making Math.random() not repeat its self.

    Hi guys and gals, just a quick question to see if I can get some help on this.

    I am making a script so I can have a dynamic menu, this menu loads external buttons at random 8 at a time, and unloads and reloads every 25 seconds.

    It works fine, except I sometimes get repeat buttons loaded.

    My code is:

    PHP Code:
    //this script will load external buttons on to a menu at random from 102 currently available.

    import flash.display.Loader
    import flash.events.Event
    import flash.net.URLRequest
     
    //this part tells the script what the file's prefix is, and also tells it the subdirectories that hold the files
    var prefix:String
    prefix "categories/indexmenu/button_"

    //this part is a timer, that tells the script to wait 25 seconds before it loads the new buttons
    var timer:Timer = new Timer(250000);
        
    timer.addEventListener(TimerEvent.TIMERtimeoff);
        
    timer.start();

    //this part tells the script how many there are total, from 0 to 101.    
    var totalfiles:Number
    totalfiles 101

    //this part tells the script what buttons to load and where.
    var loader1:Loader = new Loader(); 
    loader1.6.5
    loader1.64.5
    addChild(loader1); 

    var 
    loader2:Loader = new Loader(); 
    loader2.6.5
    loader2.111.5
    addChild(loader2); 

    var 
    loader3:Loader = new Loader(); 
    loader3.6.5
    loader3.158.5
    addChild(loader3); 

    var 
    loader4:Loader = new Loader(); 
    loader4.6.5
    loader4.205.5
    addChild(loader4);

    var 
    loader5:Loader = new Loader(); 
    loader5.6.5
    loader5.252.5
    addChild(loader5);

    var 
    loader6:Loader = new Loader(); 
    loader6.6.5
    loader6.299.5
    addChild(loader6);

    var 
    loader7:Loader = new Loader(); 
    loader7.6.5
    loader7.346.5
    addChild(loader7);

    var 
    loader8:Loader = new Loader(); 
    loader8.6.5
    loader8.393.5
    addChild(loader8);

    //this function unlods the last set of buttons and loads in the new ones.
    function loadTheFiles(totfiles:int):void
      
    loader1.unload(); 
      
    loader2.unload(); 
      
    loader3.unload();
      
    loader4.unload(); 
      
    loader5.unload(); 
      
    loader6.unload(); 
      
    loader7.unload();
      
    loader8.unload(); 
      
    loader1.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 
      
    loader2.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 
      
    loader3.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 
      
    loader4.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 
      
    loader5.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 
      
    loader6.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 
      
    loader7.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 
      
    loader8.load(new URLRequest(prefix Math.floor(Math.random() * (0101)) + ".swf")); 


    //this part triggers the unloading and loading the first time round.
    loadTheFiles(totalfiles); 

    //this part unloads and loads the buttons each time the timer expires.
    function timeoff(e:TimerEvent):void{
        
    loadTheFiles(totalfiles); 
    }

    //this part tells the main movie to stop.
    stop(); 
    My question is, how would I go about telling my script to not call the same button more than once, without having to have some monster size array.

    I plan on adding more buttons over time, so it wont repeat as often. But I was wondering if there is a better solution that just playing the odds game.

    Thanks in advance for any help.


    EDIT:

    to see a working example go to www.ranklife.com
    Last edited by HatsOff; 07-20-2009 at 10:44 PM. Reason: add example

  2. #2
    http://pat.theorigin.net cresquin's Avatar
    Join Date
    Jun 2003
    Location
    Los Angeles, CA
    Posts
    685
    make an array that has the values 0-100 (you have 100 buttons, right?). Then pull a random index from that array and remove that value shifting all the others up:

    place 10 dynamic textfields on stage named txt0, txt1...txt9 then run the following script.
    PHP Code:
    var baseArray:Array;
    var 
    numToGet:int 10;

    function 
    makeBaseArray(f_length:int):void{
        
    baseArray = new Array();
        for (var 
    i:int 0f_lengthi++){
            
    baseArray.push(i);
        }
    }

    function 
    getRandomNoRepeat():Number{
        var 
    index:int Math.random() * baseArray.length;
        var 
    value:int baseArray.splice(index1)[0];
        return 
    value;
    }

    makeBaseArray(100);

    for (var 
    i:int 0numToGeti++){
        
    this["txt" i].text getRandomNoRepeat();


  3. #3
    Member
    Join Date
    May 2009
    Posts
    66
    Thanks! ill go play about with that now.

  4. #4
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    711
    An easy way to have non-repeating random values given a list values and previous index P is
    PHP Code:
    var int = -1;

    ...

    = ( Math.floorMath.random() * ( values.length ) ) ) % values.length
    value 
    values]; 
    The idea is you've got the previous index into the list so imagine a list starting at the next element and wrapping around the end of the list to the beginning up to the element before it. You want to pick from this list thus excluding the previous element. This approach is better than the above one because it only uses arithmetic operations as opposed to array operations which are very costly.

  5. #5
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Yeah. This is why shuffle() is so great in PHP.

  6. #6
    http://pat.theorigin.net cresquin's Avatar
    Join Date
    Jun 2003
    Location
    Los Angeles, CA
    Posts
    685
    I actually have a shuffle() function in AS3, I'll have to dig it up, but it uses a similar technique to what I described above.

Tags for this Thread

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