We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Random permutations
Page Index Toggle Pages: 1
Random permutations (Read 841 times)
Random permutations
Jan 13th, 2009, 10:42pm
 
Hello,

I'm new to Processing and want to randomly permute a large array of integers.  Is there such a function in Processing (ie: the opposite of the array function sort() ) or does someone know of an efficient algorithm?

Thanks.
Re: Random permutations
Reply #1 - Jan 13th, 2009, 11:08pm
 
Shuffle is built into Java:

Code:

import java.util.Collections;

ArrayList a = new ArrayList();

a.add("A");
a.add("B");
a.add("C");

for(int i = 0; i < a.size(); i++){
 print((String)a.get(i));
}


println("");

java.util.Collections.shuffle(a);

for(int i = 0; i < a.size(); i++){
 print((String)a.get(i));
}


println("");

java.util.Collections.shuffle(a);

for(int i = 0; i < a.size(); i++){
 print((String)a.get(i));
}


Handy, no? Smiley

source:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html
Re: Random permutations
Reply #2 - Jan 14th, 2009, 12:15am
 
Thanks a lot.  It seems that did the trick.  Never in a million years would I have found (or even recognized had I found) that Java utility.

You've also shown me that had I searched for 'shuffle' instead of 'permutation' I would have found the answer in previous posts.
Re: Random permutations
Reply #3 - Jan 14th, 2009, 12:56am
 
You're welcome.  I'm also guilty of not looking it up Smiley
Page Index Toggle Pages: 1