PDA

View Full Version : i just want a damn random number, no repeats, how hard could this really be?



jynx
August 28th, 2008, 01:18 AM
i have been trying to figure this out for a few hours now. actionscript seems to have no main method, and i cant seem to follow its flow. i have been trying to generate a non repeated random number for a couple of hours now and im getting extremely pissed off that its not working.

// define pictures
pic_arr = ["images/goldstein.png", "images/kyle.png", "images/timmy.png"];
var oldRN:Number = 10;
player_mc.createEmptyMovieClip("holder1",2);
this.onLoad(display());

function display() {
ranNum = Math.floor(Math.random()*pic_arr.length);
while(ranNum == oldRN){
ranNum = Math.floor(Math.random()*pic_arr.length);
}
trace(ranNum);
oldRN = ranNum;
player_mc.holder1.loadMovie(pic_arr[ranNum]);
};

takethetrain
August 28th, 2008, 10:53 AM
I only see you calling the display() function once... how are you getting repeats when you only get a number one time? Are you using the display function somewhere else in your code that you aren't posting here?

glosrfc
August 28th, 2008, 12:17 PM
A better method would be to randomly sort the array:

var pic_arr:Array = new Array("images/goldstein.png", "images/kyle.png", "images/timmy.png");
pic_arr.sort(function () {
return random(2) ? true : false;
});
trace(pic_arr);

Depending on what exactly it is you're trying to achieve, you can either select the first item of the random array:


function display() {
player_mc.holder1.loadMovie(pic_arr[0]);
}
or else move sequentially through the array displaying each image in turn:

count = pic_arr.length;
i = 0;
function display() {
if (i < count) {
i++;
} else {
i = 0;
}
player_mc.holder1.loadMovie(pic_arr[i]);
}

jynx
August 28th, 2008, 08:57 PM
i want a constant display of random player photos, every X amount of seconds. that is the main goal. but my problem here was that I couldn't figure out how to get no repeats. I wish there was a main method in action script, it would be so much better. this is part of a flash header for a website

jynx
August 28th, 2008, 09:21 PM
glosrfc, i tried your way of sorting the array, but this does not stop repeating numbers.

glosrfc
August 28th, 2008, 11:03 PM
It's impossible for it to repeat numbers...it takes the existing array elements and sorts them into a randomly shuffled version, e.g. it takes A, B, C and shuffles them to only one of the following permutations:
A, B, C - A, C, B - B, A, C - B, C, A - C, A, B - and C, B, A.

With only three elements, it's possible that any subsequent shuffle would repeat any of the above permutations so you might get A, C, B followed by A, C, B...but every individual shuffle can only produce a single randomised array. I suspect that the reason you're getting duplications is because you've included the code in the display() function (or some other repeating function) and so it's getting shuffled more often than it needs to be.

As I mentioned above, you need to shuffle the array just once, and then sequentially load each element of the array in turn (or use Array.shift() to remove the first element and return its value) with a gap of X seconds between them until the end of the array is reached.

jynx
August 29th, 2008, 12:50 PM
ahh yes, now i see. thanks a lot!