title: How do I generate random integers within a specific range in Java?
body: <p>How do I generate a random <code>int</code> value in a specific range?</p>
  <hr />
  <p>The following methods have bugs related to integer overflow:</p>
  <pre><code>randomNum = minimum + (int)(Math.random() * maximum);
  
  // Bug: `randomNum` can be bigger than `maximum`.
  </code></pre>
  
  <pre><code>Random rn = new Random();
  int n = maximum - minimum + 1;
  int i = rn.nextInt() % n;
  randomNum =  minimum + i;
  
  // Bug: `randomNum` can be smaller than `minimum`.
  </code></pre>