Watching a D Shiffman tutorial on probability prompted me to try this.
I want to shuffle a colorList array of lerped colors. Search turned up to use IntList and shuffle. But in setup for loop I thought to replace
k++;
with
k = int (random (listColors.length));
This actually seems to work sometimes. But is inconsistent.
I’m curious if anyone knows why this would sometimes indeed shuffle the colors?
Basically what’s going on under the hood.
And is there anything I could adjust for a consistent shuffle following this line of thought? Or do I indeed have to use IntList and shuffle?
///////////////////////
/* ???How to shuffle lerped colors in an array??? */
int num = 6;
int x = 20, y = x;
int w = 360/num, h = 360;
float step = 1.0f/(num-1); // **must use a float number to return a float number**
Cell [] cells = new Cell [num];
color from = color(random(255), random(255), random(255));
color to = color(random(255), random(255), random(255));
color [] listColors = new color [num];
void setup() {
size (400, 400);
for (int k = 0; k < listColors.length; k++) { // for loop to generate an array color [] listColors of random colors
listColors[k] = lerpColor (from, to, k*step);
println(listColors);
}
int k = 0;
for (int i = 0; i < num; i++) {
cells[i] = new Cell(x+i*w, y, w, h, listColors[k]);
k = int (random (listColors.length)); // trying to shuffle the listColors
// after they have been lerped,
// this sometimes works but more often not
}
}
void draw() {
background (51);
for (int i = 0; i < num; i++) {
cells[i].display();
}
}
//////////////////////////////
class Cell {
float x, y, w, h;
color clr;
Cell (
float tempX, float tempY,
float tempW, float tempH, color tempClr) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
clr = tempClr;
}
void display() {
stroke(255);
fill(clr);
rect(x, y, w, h);
}
}
Version below uses IntList and shuffle for the solution: This appears to be working so I guess I have this set up correctly?
int num = 9;
int x = 20, y = x;
int w = 360/num, h = 360;
float step = 1.0f/(num-1); // **must use a float number to return a float number**
Cell [] cells = new Cell [num];
color from = color(random(255), random(255), random(255));
color to = color(random(255), random(255), random(255));
color [] listColors = new color [num];
IntList inventory = new IntList ();
void setup() {
size (400, 400);
for (int k = 0; k < listColors.length; k++) { // for loop to generate an array color [] listColors of random colors
listColors[k] = lerpColor (from, to, k*step);
println(listColors);
}
inventory.append(listColors);
inventory.shuffle();
int[] shuffledInventory = inventory.array();
println(shuffledInventory);
int k = 0;
for (int i = 0; i < num; i++) {
cells[i] = new Cell(x+i*w, y, w, h, shuffledInventory[k]);
k++;
}
}
void draw() {
background (51);
for (int i = 0; i < num; i++) {
cells[i].display();
}
}