title: Generate random number with given probability matlab
body: <p>I want to generate a random number with a given probability but I'm not sure how to:</p>
  
  <p>I need a number between 1 and 3</p>
  
  <pre><code>num = ceil(rand*3);
  </code></pre>
  
  <p>but I need different values to have different probabilities of generating eg.</p>
  
  <pre><code>0.5 chance of 1
  0.1 chance of 2
  0.4 chance of 3
  </code></pre>
  
  <p>I'm sure this is straightforward but I can't think of how to do it.</p>