title: Generating a triangular distribution in Matlab
body: <p>I have attempted to generate a triangular probability distribution in Matlab, but was not successful. I used the formula at <a href="http://en.wikipedia.org/wiki/Triangular_distribution" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Triangular_distribution</a>.</p>
  
  <pre><code>n = 10000000;
  
  a = 0.2;
  b = 0.7;
  c = 0.5;
  
  u = sqrt(rand(n, 1));
  
  x = zeros(n, 1);
  for i = 1:n
      U = u(i);
      if U &lt; (c-a)/(b-a)
          X = a + sqrt(U*(b-a)*(c-a));
      else
          X = b - sqrt((1-U)*(b-a)*(b-c));        
      end
      x(i) = X;
  end
  
  hist(x, 100);
  </code></pre>
  
  <p>The histogram looks like so:</p>
  
  <p><img src="https://i.stack.imgur.com/LStFa.jpg" alt="enter image description here"></p>
  
  <p>Doesn't look like much of a triangle to me. What's the problem? Am I abusing <code>rand(n)</code>?</p>