xxxxxxxxxx
40
let CHOICES = 12;
let TERMS = 2;
let count = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 2 * CHOICES; i++) {
count.push(0);
}
drawRandom();
}
function draw() {
background(255);
let maxY = max(count);
for (let i = 0; i < count.length; i++) {
let x = i * (width / count.length);
let h = map(count[i], 0, maxY, 0, -height);
let c = map(count[i], maxY / 4, maxY, 0, 255, true);
fill(c, 0, 255 - c);
rect(x, height, width / count.length, h);
}
}
function drawRandom() {
for (let i = 0; i < 500; i++) {
let y = 0;
for (let c = 0; c < TERMS; c++) {
y += floor(random(CHOICES));
}
count[y] = count[y] + 1;
}
}
function mouseClicked() {
drawRandom();
}