# Random Numbers but no duplicits

**URL:** https://forum.kirupa.com/t/random-numbers-but-no-duplicits/265748
**Category:** flash
**Created:** [July 11, 2008, 9:56pm UTC](https://forum.kirupa.com/t/random-numbers-but-no-duplicits/265748 "2008-07-11T21:56:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![brostd](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@brostd](https://forum.kirupa.com/u/brostd)
#### Post date: [July 11, 2008, 9:56pm UTC](https://forum.kirupa.com/t/random-numbers-but-no-duplicits/265748/1 "2008-07-11T21:56:14Z")

</div>

I am trying to create 3 random numbers with no doubles. i am using this code:

```auto
for (i=0; i<10; i++) {
 temp_array.push(i);
}
for (j=0; j<3; j++) {
 var index:Number = Math.floor(Math.random()*temp_array.length);
 choosenNumbers.push(temp_array[index]);
 temp_array.splice(index, 1);
}
trace(choosenNumbers);

```

I get the random numbers I want but I seem to get doubles quite a bit. Any help???

---

<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: [July 11, 2008, 10:05pm UTC](https://forum.kirupa.com/t/random-numbers-but-no-duplicits/265748/2 "2008-07-11T22:05:29Z")

</div>

```auto
var arr:Array = [1,2,3,4,5,6,7,8,9]
arr.sort(shuffle);
trace(arr);

function shuffle():Number {
	return Math.floor(Math.random() * 3) - 1;
}

```

You should be able to modify the script so that it fits your needs. Basically, just put the possible numbers in the array, sort it using `shuffle`, and pick any 3 elements in the array and they will be pseudo-random.

---

<div class="post-metadata">

### Author: ![brostd](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@brostd](https://forum.kirupa.com/u/brostd)
#### Post date: [July 11, 2008, 10:13pm UTC](https://forum.kirupa.com/t/random-numbers-but-no-duplicits/265748/3 "2008-07-11T22:13:17Z")

</div>

Yes I see how that could work, the problem i run into is that i am going to be adding to this array for pretty much ever… With this script i forgot to mention that i want to pull the max number from an array that has other info pertaining to the videos i want to display. so i was hoping that every time i added a new element to my video array i wouldn’t have to add another number…

---

<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: [July 11, 2008, 10:19pm UTC](https://forum.kirupa.com/t/random-numbers-but-no-duplicits/265748/4 "2008-07-11T22:19:49Z")

</div>

In that case, you could try making another array (or assigning a property on the existing element, if the datatype allows that) that would keep track of which videos had been played already, and then check that value when randomly selecting a video.

Would it be possible for you to kick the already selected elements out of the array? That would make it easier for you to code something to select one randomly.

---

<div class="post-metadata">

### Author: ![brostd](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@brostd](https://forum.kirupa.com/u/brostd)
#### Post date: [July 11, 2008, 10:26pm UTC](https://forum.kirupa.com/t/random-numbers-but-no-duplicits/265748/5 "2008-07-11T22:26:08Z")

</div>

Whats happening is i have a video play and then after its done three thumbnail images come up and after 5 seconds 3 new ones come up. so what i have that random number generator doing is pick 3 numbers from the list of numbers that is made. If i get 1,4,8 that is fine the problem i am running into is i will get 1,4,1. When I get the two 1’s i am basically linking to the same video and that looks bad. So i need to find out how to make sure i get 3 totally different number at a time and then after that do it a gain 5 seconds later.
