xxxxxxxxxx
28
let randomCount = []; //array to keep track of how often random numbers are picked
let total = 20;
function setup() {
createCanvas(560,390);
for (let i = 0; i < total; i++) {
randomCount[i] = 0;
}
}
function draw() {
background(220);
let randomNum = floor(random(total)); //pick a random number
randomCount[randomNum]++; //increase count
//set rectangle's color
stroke(63,63,147);
strokeWeight(2);
fill(255);
let w = width/randomCount.length; // 560/20 = 28
//draw a rectangle at location x,y (x*w,height-randomCount[x])
//with a width and height of (w-1,randomCount[x])
for (let x = 0; x < randomCount.length; x++) {
rect(x*w,height-randomCount[x],w-1,randomCount[x]);
}
}