xxxxxxxxxx
75
// Exercise 0.6
// Use a custom probability distribution to vary the size of the random walker’s steps.
//The step size can be determined by influencing the range of values picked with a qualifying random value.
//Can you map the probability to a quadratic function by making the likelihood that a value is picked equal to the value squared?
function mousePressed() {
setup();
}
function instructions() {
textAlign(CENTER, CENTER);
textSize(24)
noStroke()
fill(70)
text('click mouse to restart', width/2, height/2);
}
function acceptreject() {
while (true) {
let r1 = random(1);
// Making the probability of a value being picked equal to the value squared..?
let probability = sq(r1);
let r2 = random(1);
if (r2 < probability) {
return r1;
}
}
}
class Walker {
constructor() {
this.x = width / 2;
this.y = height / 2;
}
step() {
let step = acceptreject() * 4;
let stepx = random(-step, step);
let stepy = random(-step, step);
this.x += stepx;
this.y += stepy;
}
show() {
stroke(220);
strokeWeight(4);
point(this.x, this.y);
}
}
function setup() {
createCanvas(400, 400);
background(20);
instructions();
walker = new Walker();
}
function draw() {
walker.step();
walker.show();
}