title: Java: random integer with non-uniform distribution
body: <p>How can I create a random integer <code>n</code> in Java, between <code>1</code> and <code>k</code> with a &quot;linear descending distribution&quot;, i.e. <code>1</code> is most likely, <code>2</code> is less likely, <code>3</code> less likely, ..., <code>k</code> least likely, and the probabilities descend linearly, like this:</p>
  <p><img src="https://i.stack.imgur.com/3aNYw.jpg" alt="enter image description here" /></p>
  <p>I know that there are dozens of threads on this topic already, and I apologize for making a new one, but I can't seem to be able to create what I need from them. I know that using <code>import java.util.*;</code>, the code</p>
  <pre><code>Random r=new Random();
  int n=r.nextInt(k)+1;
  </code></pre>
  <p>creates a random integer between <code>1</code> and <code>k</code>, distributed uniformly.</p>
  <p><strong>GENERALIZATION:</strong> Any hints for creating an arbitrarily distributed integer, i.e. <code>f(n)=some function</code>, <code>P(n)=f(n)/(f(1)+...+f(k))</code>), would also be appreciated, for example:</p>
  <p><img src="https://i.stack.imgur.com/1wjO1.jpg" alt="enter image description here" />.</p>