)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Random Trick

  1. // multiply math random by another math random:
  2. const value = Math.random() * Math.random() * 10;
  3.  
  4. // This makes it less likely that `value` will be 10
  5.  
  6. // the more times you multiply, the less likely you will be to reach
  7. // the maximum potential value
  8. const value = Math.random() * Math.random() * Math.random() * 10;

I use this a fair amount when creating artwork with code. I’ll usually use a seeded random though for more control.

I was thinking about different icons I might use in a node based programming environment to denote this use of random and after a little futzing around created this series of histograms:

  1. function rand(n = 1, f = 1) {
  2.   let value = 1;
  3.   for (let i = 0; i < n; i++) {
  4.     value *= Math.random();
  5.   }
  6.   // fixing the value so that it fits
  7.   // nicely as a key in the hash table
  8.  
  9.   // hash = {
  10.   //   0.2: 5,
  11.   //   0.9: 8,
  12.   //   etc...
  13.   // }
  14.   return value.toFixed(f);
  15. }
  16.  
  17. function histo(n = 1, f = 2, off, iter = 1500, size = 70) {
  18.   const vals = {};
  19.   const canvas = document.createElement('canvas');
  20.   const c = canvas.getContext('2d');
  21.  
  22.   canvas.width = canvas.height = size;
  23.   canvas.style.margin = '.5em';
  24.   document.body.appendChild(canvas);
  25.  
  26.   for (let i = 0; i < iter; i++) {
  27.     const randNum = off ? off - rand(n, f) : rand(n, f);
  28.     if (vals[randNum] == null) {
  29.       vals[randNum] = 1;
  30.     } else {
  31.       vals[randNum]++;
  32.     }
  33.   }
  34.  
  35.   c.fillStyle = '#ccc';
  36.   c.fillRect(0, 0, size, size);
  37.   c.strokeRect(0, 0, size, size);
  38.   for (let i in vals) {
  39.     const x = parseFloat(i) * size;
  40.     c.beginPath();
  41.     c.moveTo(x, size);
  42.     c.lineTo(x, size - vals[i]);
  43.     c.stroke();
  44.   }
  45. }
  46.  
  47. histo();
  48. histo(2);
  49. histo(3, 2, 1);
  50. histo(5, 2, 1);
  51. histo(6, 2, 0, 500);
snippet.zone ~ 2021-24 /// {s/z}