# Random non-repeating number

**URL:** <https://forum.kirupa.com/t/random-non-repeating-number/261029>\
**Category:** flash\
**Created:** [May 22, 2008, 7:51am UTC](https://forum.kirupa.com/t/random-non-repeating-number/261029 "2008-05-22T07:51:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobllama](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@bobllama](https://forum.kirupa.com/u/bobllama)\
**Post date:** [May 22, 2008, 7:51am UTC](https://forum.kirupa.com/t/random-non-repeating-number/261029/1 "2008-05-22T07:51:51Z")

</div>

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:

```auto
_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

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [May 22, 2008, 9:16am UTC](https://forum.kirupa.com/t/random-non-repeating-number/261029/2 "2008-05-22T09:16:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bobllama](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@bobllama](https://forum.kirupa.com/u/bobllama)\
**Post date:** [May 22, 2008, 4:24pm UTC](https://forum.kirupa.com/t/random-non-repeating-number/261029/3 "2008-05-22T16:24:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [May 22, 2008, 5:20pm UTC](https://forum.kirupa.com/t/random-non-repeating-number/261029/4 "2008-05-22T17:20:55Z")

</div>

```auto
// 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();

```

---

<div class="post-metadata">

**Author:** ![bobllama](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@bobllama](https://forum.kirupa.com/u/bobllama)\
**Post date:** [May 22, 2008, 6:28pm UTC](https://forum.kirupa.com/t/random-non-repeating-number/261029/5 "2008-05-22T18:28:42Z")

</div>

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?

```auto
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();
	}
};

```

---

<div class="post-metadata">

**Author:** ![bobllama](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@bobllama](https://forum.kirupa.com/u/bobllama)\
**Post date:** [May 22, 2008, 9:57pm UTC](https://forum.kirupa.com/t/random-non-repeating-number/261029/6 "2008-05-22T21:57:05Z")

</div>

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

```auto

//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;
	}
};

```
