Friday, June 30, 2017

Example 11 - Random

// Example 11 - Random

// globals
var a,b,c,d,choices;

function setup() {

  createCanvas(640, 480);

  // no stroke, green fill
  noStroke();
  textSize(18);

  choices = ["A","AB","BA","C"];
  setRandom();
}

// Each frame print values for random values
function draw() {
  background(192);
  text("random(-5,5) = " + a, 0.25*width, 0.2*height);
  text("random(5) = " + b, 0.25*width, 0.4*height);
  text("random() = " + c, 0.25*width, 0.6*height);
  text("random(choices) = " + d, 0.25*width, 0.8*height);
}

// call setRandom whenever mouse is clicked
function mousePressed() {
  setRandom();
}

// new values for the random variables
function setRandom(){
  a = random(-5,5);
  b = random(5);
  c = random();
  d = random(choices);
}

Output:


No comments:

Post a Comment