How to Pick Random Unique Elements out of an Array

I saw a post on Kirupa’s Flash 8 forum recently asking how to display x random unique images from an array of y images. It sounded like a good free-time exercise so I took it on.

Essentially, all we really need to do here is pick x unique random numbers between 0 and the image array length. We can then use the random number as the array index to the image element. Picking the random numbers is easily done using Math.random. The only slight challenge here was to make sure they were unique.

In my example I will be choosing 3 random numbers from 10 array elements.

Here’s what I came up with:


var myImages:Array = new Array("pic1.jpg","pic2.jpg","pic3.jpg","pic4.jpg","pic5.jpg","pic6.jpg","pic7.jpg","pic8.jpg","pic9.jpg","pic10.jpg");
var topThree:Array = new Array();
var randomNumber:Number;
var dupe:Boolean = false;
var i:Number;
var j:Number;

for (i=0;i<3;i++) {
	dupe=false;
	randomNum = Math.floor(Math.random() * (myImages.length - 1 + 1)) + 1;
	if (topThree.length == 0) {
		topThree.push(randomNum);
	} else {
		for(j=0;j

Have an easier way? Feel free to post it.

-rG

10 thoughts on “How to Pick Random Unique Elements out of an Array

  1. Hi rG!
    Your approach is good, and it seems to work well. To optimize it a bit, I would consider taking your source array of y images and randomizing it instead. With your source array randomized, you can simply take the first x items from the array without spending time worrying about duplicates.

    You can see my approach for randomizing a list (be sure to read the comments for suggestions on improving it): http://blog.kirupa.com/?p=111

    Cheers!
    Kirupa 🙂

  2. var myImages:Array = [“pic1.jpg”, “pic2.jpg”, “pic3.jpg”, “pic4.jpg”, “pic5.jpg”, “pic6.jpg”, “pic7.jpg”, “pic8.jpg”, “pic9.jpg”, “pic10.jpg”];
    var randomImages:Array = [];
    var randomCount:Number = 3;
    var r:Number;
    for (var i = 0; i<randomCount; i++) {
    r = Math.floor(Math.random()*myImages.length);
    randomImages[randomImages.length] = myImages.splice(r, 1);
    }
    trace(randomImages);

  3. @Kirupa: Thanks for the post and the link! I’m usually the one posting on your site. Your contributions to the community are very much appreciated (even if you’re working for Microsoft now) ;).

    @sck: Thanks for the info. I’ll have to look into the sort method. The problem here though is that there may be buttons that are coded to reference the original element index. Resorting them would cause those references to refer to the wrong element.

    @Kevin: Interesting approach but part of the goal (which I didn’t mention in the post) was to keep the image array intact. I need all of the elements to remain in the array.

  4. I need help desperately!!! I am using something similar to what Keven posted but I am using it to display the images that were randomly selected. I am getting that to work but I can’t get it to stop displaying the images after it has reached the amount specified. meaning that if I give it a var randomCount:Number = 3; I want it to only flash and display the 3 images on after another in the same postion then stop & have nothing there?? This is the code that I am using:

    var myImages:Array = [“f1_sm.jpg”, “f2_sm.jpg”, “f3_sm.jpg”, “f4_sm.jpg”, “f5_sm.jpg”, “f6_sm.jpg”];
    var randomImages:Array = [];
    var randomCount:Number = 3;
    var r:Number;
    for (var i = 0; i

  5. my actions cut off before, here it is in full:

    var myImages:Array = [“f1_sm.jpg”, “f2_sm.jpg”, “f3_sm.jpg”, “f4_sm.jpg”, “f5_sm.jpg”, “f6_sm.jpg”];
    var randomImages:Array = [];
    var randomCount:Number = 3;
    var r:Number;
    for (var i = 0; i

  6. sorry its late and it has been a long weekend here it is for the third time:


    var myImages:Array = ["f1_sm.jpg", "f2_sm.jpg", "f3_sm.jpg", "f4_sm.jpg", "f5_sm.jpg", "f6_sm.jpg"];
    var randomImages:Array = [];
    var randomCount:Number = 3;
    var r:Number;
    for (var i = 0; i

  7. Hi
    How about using your exemple by using a unique button, when clicked upon, will show the images. Unique images. For exemple, instead of tracing them, I could see the actual images. Every time, different images not duplicated. Let’s choose 5 images among let’s say 50. Beside the button, what do we need to add to the stage, to make those 5 images appear in a sequence.

Comments are closed.