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 › array_shuffle equivalent in P5
Page Index Toggle Pages: 1
array_shuffle equivalent in P5? (Read 958 times)
array_shuffle equivalent in P5?
Nov 9th, 2007, 2:49am
 
Is there an equivalent of PHPs array_shuffle() function in Processing? This would come in extremely handy. I'm currently experimenting with colors. When I have an array of color values, I don't want to sort() the array - the results would look boring and "too predictable" for what I'm trying to achieve. Rather, I'd just have a random sequence instead.
Re: array_shuffle equivalent in P5?
Reply #1 - Nov 9th, 2007, 6:43am
 
For such problems where there's no built-in function in Processing, do a research for Java methods in general :

You can use Collections.shuffle() method to shuffle a collection - or an array using Arrays.asList() method.

Code:
String[] array = new String[]{"1", "2", "3"};
println(array[0] + " / " + array[1] + " / " + array[2]);
Collections.shuffle(Arrays.asList(array));
println(array[0] + " / " + array[1] + " / " + array[2]);
Re: array_shuffle equivalent in P5?
Reply #2 - Nov 9th, 2007, 7:07am
 
Thanks, that's just what I was looking for. Coming from the Actionscript / JS / PHP world, I still gotta find my way around the Java docs. I was looking up "array" as keyword, guessing that a matching method would be listed there...
Re: array_shuffle equivalent in P5?
Reply #3 - Nov 9th, 2007, 7:30am
 
hmmm... while your example works fine with ints or strings, the P5 compiler throws an error when I define a hex-color array.


int maxpal = 1024;
size(1200, 1200);
background(255);

int middle = width/2;
smooth();

color[] goodcolor = new color[]{#FEFEFE, #FEFEFD, #FBFEF9, #FCFEFC, #FCFEFB, #FEFEFC, #FEFDF9 ... goes on and on and on...};
color[] c = subset(goodcolor, 300); // cut off first 300 colors (most likely white-ish)

Collections.shuffle(Arrays.asList(c)); // randomize array

for (int i = 0; i < c.length; i++) {
strokeWeight(5);
stroke(c[i]);
noFill();
//ellipse(middle,  middle,  width-(i+5),  height-(i+5)); // from outside to center
ellipse(middle, middle, i*1.4, i*1.4); // from center to outside
} // end loop

//save("colorCircle.tif");



Semantic Error: No applicable overload for a method with signature "asList(int[])" was found in type "java.util.Arrays". Perhaps you wanted the overloaded version "java.util.List asList(java.lang.Object... $1);" instead?


I've tried using List, but that didn't work either. Is there any way around that? Do I have to convert the hex values to something else first?
Re: array_shuffle equivalent in P5?
Reply #4 - Nov 9th, 2007, 8:39am
 
int is a basic data-type not an object ( class that extends java.lang.Object ). since asList requires Object[] (see error) you'd have to use Integer[] instead. now eveytime you would use one of these Integers as color you'd have to do Integer.intValue() which complicates things a bit:

Code:


Integer[] toShuffle = new Integer[]{
new Integer(1),
new Integer(2),
new Integer(3),
new Integer(4),
new Integer(5),
new Integer(6)
};

Collections.shuffle(Arrays.asList(toShuffle));

println( toShuffle );

println( toShuffle[0].intValue() );


i recommend writing your own shuffle function .. something like:

Code:

int[] toShuffle = {
0,1,2,3,4,5,6,7,8};

int[] shuffled = new int[toShuffle.length];

int i=0;

while ( toShuffle.length > 0 )
{
int rnd = int(random(toShuffle.length)); // by default random() never returns the given max value
shuffled[i] = toShuffle[rnd];
i++;
if ( rnd > 0 && rnd < toShuffle.length-1 )
toShuffle = concat(subset(toShuffle,0,rnd), subset(toShuffle,rnd+1,toShuffle.length-rnd-1));
else if ( rnd == 0 )
toShuffle = subset(toShuffle,1,toShuffle.length-1);
else
toShuffle = shorten( toShuffle );
}

println( shuffled );
Page Index Toggle Pages: 1