-
Oso polar
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!
-
-
Oso polar
Heh, I tought it would be "shorter". Thank you very much, Oldnewbie!
-
Senior Member
I would recommend that you get in the habit of using Math.floor() instead of Math.round() when working with random numbers.
Instead of this:
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).
Here's a link to a thread on the subject:
http://www.flashkit.com/board/showth...hreadid=574545
- Jim
Last edited by jbum; 09-27-2004 at 08:14 PM.
-
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.
onClipEvent(load){
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.
-
 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.
Instead of this:
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).
Here's a link to a thread on the subject:
http://www.flashkit.com/board/showth...hreadid=574545
- 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{
removeSubmenuItems();
removesubSubmenuItems();
// build a new submenu...
var i:Number = 0;
var n:Number = this.submenus.length;
// submenus will be an array contain the submenu nodes
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.txtcon_mc.subItem_txt.text = this.submenus[i].attributes.title;
sub_mc.subSubItems = this.submenus[i].childNodes;
sub_mc.onRelease = onSubItemRelease;
i++;
// add each submenu item to the array
submenuItems.push( sub_mc );
}
};
the current random code is neither unique or really random
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|