Re: [flexcoders] Newbie Question - Random position in an array
Note that this will break on sparse arrays, though. In Flex, it's perfectly valid to have something like var foo:Array = []; foo[0] = 2; foo[3] = 5; This array has length 4, but only two elements. Elements 1 and 2 evaluate to 'undefined'. I'm not sure if there's an easy way to pick a random element out of a sparse array. If you want something unscientific, I suppose you can just increment the randomly chosen index until you find something. Or randomly pick again (although with *really* sparse arrays, this could theoretically be a perf problem). -- Maciek Sakrejda Truviso, Inc. http://www.truviso.com -Original Message- From: Sean Scott [EMAIL PROTECTED] Reply-To: flexcoders@yahoogroups.com To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Newbie Question - Random position in an array Date: Sat, 1 Mar 2008 22:55:49 -0600 Sherif, Thanks i ended up with var randSeed:Number = new Number(Math.floor(Math.random() * this.techniColors.length)); On Sat, Mar 1, 2008 at 9:48 PM, Sherif Abdou [EMAIL PROTECTED] wrote: something like this should work var randomNumber:int = Math.Random * myArray.length; then do something like myArray[randomNumber] whatever - Original Message From: Sean Scott [EMAIL PROTECTED] To: flexcoders@yahoogroups.com Sent: Saturday, March 1, 2008 9:39:39 PM Subject: [flexcoders] Newbie Question - Random position in an array Hi folks, wondering if someone has a function that returns a random position from an array. Thanks from a newbie, Never miss a thing. Make Yahoo your homepage.
Re: [flexcoders] Newbie Question - Random position in an array
Maciek thanks for clearing that up, but in this case the array being access was created with known values. so no possibility of sparsity. On Mon, Mar 3, 2008 at 10:27 AM, Maciek Sakrejda [EMAIL PROTECTED] wrote: Note that this will break on sparse arrays, though. In Flex, it's perfectly valid to have something like var foo:Array = []; foo[0] = 2; foo[3] = 5; This array has length 4, but only two elements. Elements 1 and 2 evaluate to 'undefined'. I'm not sure if there's an easy way to pick a random element out of a sparse array. If you want something unscientific, I suppose you can just increment the randomly chosen index until you find something. Or randomly pick again (although with *really* sparse arrays, this could theoretically be a perf problem). -- Maciek Sakrejda Truviso, Inc. http://www.truviso.com -Original Message- From: Sean Scott [EMAIL PROTECTED] sean.net%40gmail.com Reply-To: flexcoders@yahoogroups.com flexcoders%40yahoogroups.com To: flexcoders@yahoogroups.com flexcoders%40yahoogroups.com Subject: Re: [flexcoders] Newbie Question - Random position in an array Date: Sat, 1 Mar 2008 22:55:49 -0600 Sherif, Thanks i ended up with var randSeed:Number = new Number(Math.floor(Math.random() * this.techniColors.length)); On Sat, Mar 1, 2008 at 9:48 PM, Sherif Abdou [EMAIL PROTECTED]sherif626%40yahoo.com wrote: something like this should work var randomNumber:int = Math.Random * myArray.length; then do something like myArray[randomNumber] whatever - Original Message From: Sean Scott [EMAIL PROTECTED] sean.net%40gmail.com To: flexcoders@yahoogroups.com flexcoders%40yahoogroups.com Sent: Saturday, March 1, 2008 9:39:39 PM Subject: [flexcoders] Newbie Question - Random position in an array Hi folks, wondering if someone has a function that returns a random position from an array. Thanks from a newbie, __ Never miss a thing. Make Yahoo your homepage.
Re: [flexcoders] Newbie Question - Random position in an array
something like this should work var randomNumber:int = Math.Random * myArray.length; then do something like myArray[randomNumber] whatever - Original Message From: Sean Scott [EMAIL PROTECTED] To: flexcoders@yahoogroups.com Sent: Saturday, March 1, 2008 9:39:39 PM Subject: [flexcoders] Newbie Question - Random position in an array Hi folks, wondering if someone has a function that returns a random position from an array. Thanks from a newbie, Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Re: [flexcoders] Newbie Question - Random position in an array
Sherif, Thanks i ended up with var randSeed:Number = new Number(Math.floor( Math.random() * this.techniColors.length)); On Sat, Mar 1, 2008 at 9:48 PM, Sherif Abdou [EMAIL PROTECTED] wrote: something like this should work var randomNumber:int = Math.Random * myArray.length; then do something like myArray[randomNumber] whatever - Original Message From: Sean Scott [EMAIL PROTECTED] To: flexcoders@yahoogroups.com Sent: Saturday, March 1, 2008 9:39:39 PM Subject: [flexcoders] Newbie Question - Random position in an array Hi folks, wondering if someone has a function that returns a random position from an array. Thanks from a newbie, -- Never miss a thing. Make Yahoo your homepage.http://us.rd.yahoo.com/evt=51438/*http://www.yahoo.com/r/hs