# How to Pick Random Unique Elements out of an Array

**URL:** <https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426>\
**Category:** flash\
**Created:** [June 16, 2008, 12:35am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426 "2008-06-16T00:35:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![insultcomidog](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@insultcomidog](https://forum.kirupa.com/u/insultcomidog)\
**Post date:** [June 16, 2008, 12:35am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/1 "2008-06-16T00:35:03Z")

</div>

I have an array of sprites. I’m using the following code to apply an animation to a randomly chosen sprite in my array:

```auto

var i : int = Math.floor(Math.random() * tempTileArray.length);
var child : Tile = tempTileArray*;
child.bonusTileEffect();

```

My question is how do grab more than one random element from my array? I would like to apply my animation to x random sprites in my array.

Appreciate any help you can offer.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 16, 2008, 1:15am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/2 "2008-06-16T01:15:06Z")

</div>

You’re gonna wann splice out the tiles that you have already applied the code to and then simply run your code x amount of times.

```auto
for(var i:int = 0; i < x; i++) {
	var child : Tile = tempTileArray.splice(Math.floor(Math.random() * tempTileArray.length), 1)[0];
	child.bonusTileEffect();
}

```

---

<div class="post-metadata">

**Author:** ![insultcomidog](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@insultcomidog](https://forum.kirupa.com/u/insultcomidog)\
**Post date:** [June 16, 2008, 1:25am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/3 "2008-06-16T01:25:21Z")

</div>

Thnks for the reply TheCanadian. Your method makes sense, but I need to keep the original array intact as I have other other functions that use it. Is there another way to select x number of random elements from an array?

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 16, 2008, 1:28am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/4 "2008-06-16T01:28:47Z")

</div>

Create a temporary array that is the same as the original and perform the manipulations on that.

```auto

var myArray:Array = ["cat", "dog", "rat"]
var tempArray:Array = myArray;

trace(tempArray) //traces "cat", "dog", "rat"

```

---

<div class="post-metadata">

**Author:** ![aho](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@aho](https://forum.kirupa.com/u/aho)\
**Post date:** [June 16, 2008, 1:34am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/5 "2008-06-16T01:34:44Z")

</div>

Take a look at my continuous shuffling algorithm. It’s a _very_ elegant solution for this set of problems.  
[Never-ending Shuffled Sequences - When Random is too Random](http://kaioa.com/node/53)

If each item exists only once there can be still a repetition when it wraps around. So, that’s a special case you might want to handle. The simplest method would be to check if the currently drawn item matches the previous one and if so draw again. While that slightly skews the distribution, it’s probably good enough.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 16, 2008, 2:29am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/6 "2008-06-16T02:29:24Z")

</div>

[QUOTE=Anogar;2343300]Create a temporary array that is the same as the original and perform the manipulations on that.

```auto

var myArray:Array = ["cat", "dog", "rat"]
var tempArray:Array = myArray;

trace(tempArray) //traces "cat", "dog", "rat"

```

[/QUOTE]  
Noooooo!!! Both variables are referencing the same array.

The idea’s right though but you’ll have to make a seperate object. I use the splice method for that as well.

```auto
var a:Array = [];
var b:Array = a.splice();

trace(a == b);

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 16, 2008, 3:57am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/7 "2008-06-16T03:57:09Z")

</div>

Durrr, you’re right, ignore me and my brainfart. :lol:

I think you meant [FONT=Courier New]slice [/FONT]though, not [FONT=Courier New]splice[/FONT]. Obviously, they both work, but if you start trying to only return certain elements you’ll start ripping them out of the original.

```auto

var myArray:Array = ["cat", "dog", "rat"]
var tempArray = myArray.splice();

tempArray = [];

trace(tempArray); //traces nothing
trace(myArray); //traces the original array

```

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 16, 2008, 4:29am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/8 "2008-06-16T04:29:00Z")

</div>

Forgive my ignorance here, but wouldn’t using the **new** operator solve this without slice / splicing?

```auto
var myArray:Array = new Array('cat', 'dog','bird');
var otherArray:Array = new Array(myArray);

otherArray.push('fish');

trace (myArray == otherArray);

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 16, 2008, 4:34am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/9 "2008-06-16T04:34:07Z")

</div>

[QUOTE=Anogar;2343321]Durrr, you’re right, ignore me and my brainfart. :lol:

I think you meant [FONT=Courier New]slice [/FONT]though, not [FONT=Courier New]splice[/FONT]. Obviously, they both work, but if you start trying to only return certain elements you’ll start ripping them out of the original. [/QUOTE]  
Ignore my brainfart :hugegrin:

[QUOTE=dthought;2343324]Forgive my ignorance here, but wouldn’t using the **new** operator solve this without slice / splicing?

```auto
var myArray:Array = new Array('cat', 'dog','bird');
var otherArray:Array = new Array(myArray);

otherArray.push('fish');

trace (myArray == otherArray);

```

[/QUOTE]

No because that pushes the entire original array as the first element of the new one:

```auto
var myArray:Array = new Array('cat', 'dog','bird');
var otherArray:Array = new Array(myArray);
trace(otherArray[0] is Array); //true

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 16, 2008, 4:43am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/10 "2008-06-16T04:43:06Z")

</div>

> [@dthought;2343324](#):
>
> Forgive my ignorance here, but wouldn’t using the **new** operator solve this without slice / splicing?

Yeah, as TheCanadian mentioned, that’ll stick it as the first element in the new array, so tempArray[0] would trace the entire original array.

The reason to use slice over splice is that slice is non-destructive.

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 16, 2008, 6:20am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/11 "2008-06-16T06:20:55Z")

</div>

Aha - well yes, that makes sense. I should have put a little more thought into it. One of those days, I guess 😉

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 16, 2008, 7:17am UTC](https://forum.kirupa.com/t/how-to-pick-random-unique-elements-out-of-an-array/263426/12 "2008-06-16T07:17:35Z")

</div>

You could maybe do something like this

```auto
var indices = new Array();
var selectedSprites:Array = new Array(); // to hold the ones we pick

for(var i:int = 0; i<indices.length; i++)
{
    indices* = i;
}

for(i = 0; i < 4; i++) // say you want to pick 4 sprites
{
    // pick a random position in indices
    var r = Math.floor(Math.random()*indices.length);
    
    // get the sprite in that position
    var pickedSprite = tempTileArray[r];
    
    // add that sprite to our selection
    selectedSprites.push(pickedSprite);
    
    // remove that position from indices
    indices.splice(i,1);
    
}

```
