[Flashcoders] Howto randomize the elements of an array?
Hi, I have an array with up to 1 elements. These elements are sorted of some kind and I need to reorganize the elements so that their positions becomes absolutely random. I guess my best approach would be to assign my own sort function to the Array.sort() method which did nothing but return -1,0 or 1 randomly, but I guess it would be necessary to sort the array many times in order to make it truly random and that would take a lot of CPU power with that many elements. Is there a better way to do this from a performance point of view? Thanks in advance. Martin Baltzer ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Howto randomize the elements of an array?
I have an array with up to 1 elements. These elements are sorted of some kind and I need to reorganize the elements so that their positions becomes absolutely random. I guess my best approach would be to assign my own sort function to the Array.sort() method which did nothing but return -1,0 or 1 randomly, but I guess it would be necessary to sort the array many times in order to make it truly random and that would take a lot of CPU power with that many elements. That sounds like a very bad idea :) As you say, you'd not be getting a very well randomised list even if you did the sort lots of times. The basic randomisation algorithm is: take a new array for each element in the old array: add it at a random position in the new array It's not really rocket science, but it should do the job perfectly well for your purposes. Danny ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Howto randomize the elements of an array?
I'd do it like this: Take a new array, take a random element from the oldArray and remove it from the oldArray. Then put it as the next element in the newArray. like so: var oldArray:Array = [item1,item2,item3,item4,item5,item6]; var newArray:Array = []; while (oldArray.length) { newArray.push(oldArray.splice(Math.random()*oldArray.length,1)); } trace(newArray); // Luppis The basic randomisation algorithm is: take a new array for each element in the old array: add it at a random position in the new array It's not really rocket science, but it should do the job perfectly well for your purposes. Danny ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Howto randomize the elements of an array?
Martin, Have you looked at http://proto.layer51.com/l.aspx?p=3 There are about 10 custom methods of shuffle-like. I haven't tested them for performance though :-). -- Best regards, GregoryN http://GOusable.com Flash components development. Usability services. From: Martin Baltzer [EMAIL PROTECTED] Subject: [Flashcoders] Howto randomize the elements of an array? To: Flashcoders mailing list flashcoders@chattyfig.figleaf.com Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; charset=us-ascii Hi, I have an array with up to 1 elements. These elements are sorted of some kind and I need to reorganize the elements so that their positions becomes absolutely random. I guess my best approach would be to assign my own sort function to the Array.sort() method which did nothing but return -1,0 or 1 randomly, but I guess it would be necessary to sort the array many times in order to make it truly random and that would take a lot of CPU power with that many elements. Is there a better way to do this from a performance point of view? Thanks in advance. Martin Baltzer -- ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
SV: [Flashcoders] Howto randomize the elements of an array? [Closed]
Hi all, Thanks a lot for your help! While Mikko came with probably the fewest lines of code to solve this I found out that this method posted by iv here http://proto.layer51.com/d.aspx?f=893 (Read msg3) is far superior when it comes to speed ;-) I believe it's the use of local variables that does the trick. Cheers Martin -Oprindelig meddelelse- Fra: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] På vegne af GregoryN Sendt: 18. april 2006 14:15 Til: Flashcoders mailing list Emne: Re: [Flashcoders] Howto randomize the elements of an array? Martin, Have you looked at http://proto.layer51.com/l.aspx?p=3 There are about 10 custom methods of shuffle-like. I haven't tested them for performance though :-). -- Best regards, GregoryN http://GOusable.com Flash components development. Usability services. From: Martin Baltzer [EMAIL PROTECTED] Subject: [Flashcoders] Howto randomize the elements of an array? To: Flashcoders mailing list flashcoders@chattyfig.figleaf.com Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; charset=us-ascii Hi, I have an array with up to 1 elements. These elements are sorted of some kind and I need to reorganize the elements so that their positions becomes absolutely random. I guess my best approach would be to assign my own sort function to the Array.sort() method which did nothing but return -1,0 or 1 randomly, but I guess it would be necessary to sort the array many times in order to make it truly random and that would take a lot of CPU power with that many elements. Is there a better way to do this from a performance point of view? Thanks in advance. Martin Baltzer -- ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Howto randomize the elements of an array?
See: [Flashcoders] Array.shuffle http://chattyfig.figleaf.com/pipermail/flashcoders/2002-August/043766.html _ Danny Kodicek wrote: The basic randomisation algorithm is: take a new array for each element in the old array: add it at a random position in the new array It's not really rocket science, but it should do the job perfectly well for your purposes. Good luck, Fumio Nonaka mailto:[EMAIL PROTECTED] http://www.FumioNonaka.com/ My bookshttp://www.FumioNonaka.com/Books/index.html Flash communityhttp://F-site.org/ ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com