title: Given min-value, max-value and mean, can I elegantly generate data to fit a bell curve?
body: <p>Let's say I have an array of objects.</p>
  
  <p>I have 3 values associated with this array: min-height, max-height and average-height.</p>
  
  <p>I want to assign a height to each object so that:</p>
  
  <ul>
  <li>No object's height is less than min-height </li>
  <li>No object's height is greater than max-height </li>
  <li>The mean of all objects' heights is average-height.</li>
  </ul>
  
  <p>Essentially I am looking to generate a height distribution like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Y6qek.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y6qek.png" alt="enter image description here"></a></p>
  
  <p>The heights have to be pseudo-random - that is to say, I want to be able to get a height for each object by feeding the result of a random number generator into a function and getting the height returned.</p>
  
  <p>My solution at the moment is to split my range of acceptable heights (all between min-height and max-height) into a series of bins and assign a probability to each bin. Once a bin is selected, I choose a height from within that range at random.</p>
  
  <p>This is not an ideal solution as it is inelegant, clunky, and produces a stepped curve as opposed to a smooth one.</p>
  
  <p><a href="https://i.stack.imgur.com/vv7uM.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vv7uM.gif" alt="enter image description here"></a></p>
  
  <p>Here is my current code for producing the bins:</p>
  
  <pre><code>var min_height = 10
  var max_height = 100
  var avg_height = 30
  
  var scale = SCALE ()
      .map_from([min_height, avg_height, max_height])
      .map_to([-Math.PI, 0, Math.PI])
  
  var range = max_height - min_height;
  
  var num_of_bins = 10
  var bin_size = range/num_of_bins;
  
  var bins = []
  var sum_of_probability = 0
  
  while (bins.length &lt; num_of_bins) {
      var bin = {};
      bin.min = min_height + (bins.length*bin_size);
      bin.max = bin.min + bin_size;
      bin.mid = bin.min + (bin_size/2);
  
      bin.probability = Math.cos(scale(bin.mid))+1
      sum_of_probability += bin.probability;
  
      bins.push(bin)
  }
  
  var i;
  var l = bins.length;
  for (i=0; i&lt;l; i++) {
      bins[i].probability /= sum_of_probability
  
      if (bins[i-1]) {
          bins[i].cumulative_probability = bins[i-1].cumulative_probability + bins[i].probability;
      }
      else {
          bins[i].cumulative_probability = bins[i].probability;
      }
  }
  </code></pre>
  
  <p>Essentially I would love to be able to generate pseudo-random data to roughly fit a curve in an elegant way, and I am not sure if this is possible in javascript. Let me know if you think this is do-able.</p>