A Flash Developer Resource Site

# Thread: How to put unique (different) numbers to array

1. ## How to put unique (different) numbers to array

Hi!

Please help me, guys. I have an array of lenght 3, let's call it "myArray". Now I want to generate 3 numbers with Math.round(Math.random()*2) and put them to myArray but no number should repeat in it. So in case Math.round(Math.random()*2) would generate a number which is already in myArray, it wouldn't put it there, it would generate an other number and compare it with the numbers in array again.

Thank you for any advice or ideas!   Reply With Quote

2.  Reply With Quote

3. Heh, I tought it would be "shorter". Thank you very much, Oldnewbie!  Reply With Quote

4. I would recommend that you get in the habit of using Math.floor() instead of Math.round() when working with random numbers.

Math.round(Math.random()*2)

use this:

Math.floor(Math.random()*3)

The reason is that you probably want each number 0, 1 and 2 to have an even distribution. When you use Math.floor() then each number will occur 1/3rd of the time.

When you use Math.round, then the 0 will occur 1/6th of the time, 2 will occur 1/6th of the time, and 1 will occur 2/3rds of the time. You can see why by plotting the range 0-3 on a timeline and marking where Math.round() changes from rounding down and rounding up (at .5).

The code I use for generating an array of n numbers is a little shorter. It looks like this:

code:
```
n = 10;  # number of numbers you want in your array
ary = [];
for (i = 0; i < n; ++i) ary[i] = i;
ary.sort( function() { return random(2)? 1 : -1; });

// test it
trace(ary);
```

And while we're on the subject, if you are going to use that array to generate M unique numbers from the list of N numbers, there's a more efficient way to do it (that doesn't involve generating an array, or a lot of tests).

- Jim  Reply With Quote

5. Cheers, This code has been working but how do i change the code below so that the numbers only go from 1 - 4? I dont want the 0 in the array.

questions = 5; // number of numbers you want in your array
_root.arry = [];
for (i = 0; i < questions; ++i) _root.arry[i] = i;
_root.arry.sort( function() { return random(2)? 1 : -1; });
_root.next_one = 0;
_root.nice = _root.arry[_root.next_one]
_root.pink2.gotoAndPlay(_root.nice);
}

i am using the items in the array to go to certain frames in a movie. The gotoAndPlay at the bottom is not working and i dont know why! Im new to all this and its very confusing. Thanks.  Reply With Quote

6. Originally Posted by jbum
I would recommend that you get in the habit of using Math.floor() instead of Math.round() when working with random numbers.

Math.round(Math.random()*2)

use this:

Math.floor(Math.random()*3)

The reason is that you probably want each number 0, 1 and 2 to have an even distribution. When you use Math.floor() then each number will occur 1/3rd of the time.

When you use Math.round, then the 0 will occur 1/6th of the time, 2 will occur 1/6th of the time, and 1 will occur 2/3rds of the time. You can see why by plotting the range 0-3 on a timeline and marking where Math.round() changes from rounding down and rounding up (at .5).

The code I use for generating an array of n numbers is a little shorter. It looks like this:

code:
```
n = 10;  # number of numbers you want in your array
ary = [];
for (i = 0; i < n; ++i) ary[i] = i;
ary.sort( function() { return random(2)? 1 : -1; });

// test it
trace(ary);
```

And while we're on the subject, if you are going to use that array to generate M unique numbers from the list of N numbers, there's a more efficient way to do it (that doesn't involve generating an array, or a lot of tests).

- Jim
Can i put this in a while loop instead like
Code:
```var myArray:Array = Array("1", "2", "3","4","5");

var i:Number = 0;
var n:Number = myArray.length;
trace(myArray)

while (i < n)
{

ary[i] = i;

ary.sort( function() { return random(2)? 1 : -1; });

i++;

// test it

};```
offcourse I can't since I wouldn't have asked, but i need a random output on some XML feed to attach randome unique mc's to..

in this setup:

Code:
```function onItemRelease():Void{

var i:Number = 0;

while (i < n)
{
var ran:Number = Math.floor(Math.random() * i) + 1;
trace(ran);
var sub_mc = container2_mc.attachMovie("si0"+ran,"subItem"+i,i*10);
sub_mc._y = 40*i;

sub_mc.onRelease = onSubItemRelease;

i++;
}

};```
the current random code is neither unique or really random  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 