-
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(25000, 0); timer.addEventListener(TimerEvent.TIMER, timeoff); 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.x = 6.5; loader1.y = 64.5; addChild(loader1);
var loader2:Loader = new Loader(); loader2.x = 6.5; loader2.y = 111.5; addChild(loader2);
var loader3:Loader = new Loader(); loader3.x = 6.5; loader3.y = 158.5; addChild(loader3);
var loader4:Loader = new Loader(); loader4.x = 6.5; loader4.y = 205.5; addChild(loader4);
var loader5:Loader = new Loader(); loader5.x = 6.5; loader5.y = 252.5; addChild(loader5);
var loader6:Loader = new Loader(); loader6.x = 6.5; loader6.y = 299.5; addChild(loader6);
var loader7:Loader = new Loader(); loader7.x = 6.5; loader7.y = 346.5; addChild(loader7);
var loader8:Loader = new Loader(); loader8.x = 6.5; loader8.y = 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() * (0, 101)) + ".swf")); loader2.load(new URLRequest(prefix + Math.floor(Math.random() * (0, 101)) + ".swf")); loader3.load(new URLRequest(prefix + Math.floor(Math.random() * (0, 101)) + ".swf")); loader4.load(new URLRequest(prefix + Math.floor(Math.random() * (0, 101)) + ".swf")); loader5.load(new URLRequest(prefix + Math.floor(Math.random() * (0, 101)) + ".swf")); loader6.load(new URLRequest(prefix + Math.floor(Math.random() * (0, 101)) + ".swf")); loader7.load(new URLRequest(prefix + Math.floor(Math.random() * (0, 101)) + ".swf")); loader8.load(new URLRequest(prefix + Math.floor(Math.random() * (0, 101)) + ".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
-
http://pat.theorigin.net
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 = 0; i < f_length; i++){ baseArray.push(i); } }
function getRandomNoRepeat():Number{ var index:int = Math.random() * baseArray.length; var value:int = baseArray.splice(index, 1)[0]; return value; }
makeBaseArray(100);
for (var i:int = 0; i < numToGet; i++){ this["txt" + i].text = getRandomNoRepeat(); }
-
Thanks! ill go play about with that now.
-
Senior Member
An easy way to have non-repeating random values given a list values and previous index P is
PHP Code:
var P : int = -1;
...
P = ( P + 1 + Math.floor( Math.random() * ( values.length - 1 ) ) ) % values.length value = values[ P ];
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.
-
Senior Member
Yeah. This is why shuffle() is so great in PHP.
-
http://pat.theorigin.net
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|