int[] histogram = new int[1000];
//draw index 500 in middle of the screen
int start = 500;
int gridWidth = 1; //separate each index by 1 pixels:
int simulationSteps = 30;
void setup() {
size(100,100);
}
void draw() {
int y = height-simulationSteps * gridWidth;
for(int i = 0; i < histogram.length; i++) {
int x = xPositionFromIndex(i);
fill(255,0,0);
rect(x,y,gridWidth,-histogram[i] * gridWidth);
}
}
void simulateOne() {
int value = start;
int y = height - gridWidth;
for(int i = 0; i < simulationSteps; i++) {
if(random(0,100)<50) value--;
else value++;
y-= gridWidth;
int x = xPositionFromIndex(value);
fill(0,10);
noStroke();
rect(x,y,gridWidth,gridWidth);
}
histogram[value]++;
}
void keyPressed() {
simulateOne();
}
void mousePressed() {
simulateOne();
}
int xPositionFromIndex(int i) {
return (i-start) * gridWidth + width / 2;
}