Random non-repeating number

Greetings all.

Before I start: I know there is a lot of non-repeating random number stuff on this forum, but I can’t quite seem to tailor any of it to this situation:

I’ve got 45 sequentially-named instances of an MC on my stage (piece_1, piece_2, etc.). I’m trying to trigger them randomly in a staggered fashion, meaning when one reaches frame 4 of it’s animation, another random one starts.

I can get them to trigger sequentially using this code:

_parent.n++;
next_piece = _parent["piece_"+_parent.n];
next_piece.play();

Now the next step is to get them to trigger non-sequentially. I probably need an array for this, and my understanding and usage of them is fairly limited, especially when it comes to calling object names properly.

So my question for you is this:

How can I trigger a random MC “piece_[x]” from within the previously triggered piece?

Thanks,
Bob

you need a random number generator… who would have guessed!

My God, Pollock. You’ve cracked it wide open.

// initialization
_parent.seq = new Array();
for(var i = 1; i <= 45; i++){
	_parent.seq.push(i);
}
_parent.seq.sort(function(){ return Math.floor(Math.random() * 3) - 1});
trace(_parent.seq);

// later
_parent.n++;
next_piece = _parent["piece_"+_parent.seq[_parent.n]];
next_piece.play();

EDIT: Ok, I got it working but I suspect it’s not ideal… I think it might be running like crap (probably because of 2 for loops running simultaneously)… Anyone have any suggestions for cleaning this up?

total = 45;
pieces = new Array();
contains = function (r, pieces) {
	for (i=0; i<pieces.length; i++) {
		if (pieces* == r) {
			return 1;
		}
	}
	return -1;
};
this.onEnterFrame = function() {
	for (u=0; u>-1; u++) {
		r = Math.round(Math.random(total)*total);
		if (contains(r, pieces) == -1) {
			break;
		}
	}
	{
		pieces.push(r);
		next_piece = animation["piece_"+r];
		next_piece.play();
	};
	if (pieces.length>45) {
		delete this.onEnterFrame;
		stop();
	}
};

Well, in case anyone cares, I found a much more obvious and much more efficient method.


//Total # of items
currNum = 45;

//Add all elements to the array
pieces = new Array();
for (i=0; i<45; i++) {
	pieces* = "piece_"+(i+1);
}

this.onEnterFrame = function() {
//Generate new number
	r = Math.round(Math.random(currNum)*currNum);

//sync current number to array item
	currPiece = pieces[r];

	next_piece = animation[currPiece];
	next_piece.play();

//remove used item from array
	pieces.splice(r,1);

//reduce the total number of options
	currNum--;

//and when we run out of numbers, delete
	if (currNum<0) {
		delete this.onEnterFrame;
	}
};