Random probability (Customizing random function)

Here is an image of what I managed to get. Looks good to me. :grin:

What is even better the solution is very uncomplicated :wink:.

graph

float y_low, y_high;
float[] y;

void setup() {
  size(640, 400);
  background(255);
  y = new float[width];
  y_low = 0;
  y_high = height / 2;
  // Draw th graph
  stroke(64);
  for (int i = 0; i < y.length; i++) {
    y[i] = randomLinear(y_low, y_high);
    line(i, height / 2 - y[i], i, height / 2);
  }
  // Draw the sorted graph
  y = sort(y);
  stroke(255, 10, 100);
  for (int i = 0; i < y.length; i++) {
    line(i, height - y[i], i, height);
  }
}  

/**
 Return a random value >= low and < high such
 that the distribution is proportional to the 
 returned value.
 */
float randomLinear(float low, float high) {
  float r = sqrt(2) * random(1/sqrt(2));
  return r * (high - low) + low;
}
2 Likes