Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In <strong>Java 1.7 or later</strong>, the standard way to do this is as follows:</p> <pre><code>import java.util.concurrent.ThreadLocalRandom; // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); </code></pre> <p>See <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadLocalRandom.html#nextInt-int-int-" rel="noreferrer">the relevant JavaDoc</a>. This approach has the advantage of not needing to explicitly initialize a <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Random.html" rel="noreferrer">java.util.Random</a> instance, which can be a source of confusion and error if used inappropriately.</p> <p>However, conversely there is no way to explicitly set the seed so it can be difficult to reproduce results in situations where that is useful such as testing or saving game states or similar. In those situations, the pre-Java 1.7 technique shown below can be used.</p> <p><strong>Before Java 1.7</strong>, the standard way to do this is as follows:</p> <pre><code>import java.util.Random; /** * Returns a pseudo-random number between min and max, inclusive. * The difference between min and max can be at most * &lt;code&gt;Integer.MAX_VALUE - 1&lt;/code&gt;. * * @param min Minimum value * @param max Maximum value. Must be greater than min. * @return Integer between min and max, inclusive. * @see java.util.Random#nextInt(int) */ public static int randInt(int min, int max) { // NOTE: This will (intentionally) not run as written so that folks // copy-pasting have to think about how to initialize their // Random instance. Initialization of the Random instance is outside // the main scope of the question, but some decent options are to have // a field that is initialized once and then re-used as needed or to // use ThreadLocalRandom (if using at least Java 1.7). // // In particular, do NOT do 'Random rand = new Random()' here or you // will get not very good / not very random results. Random rand; // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive int randomNum = rand.nextInt((max - min) + 1) + min; return randomNum; } </code></pre> <p>See <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-" rel="noreferrer">the relevant JavaDoc</a>. In practice, the <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Random.html" rel="noreferrer">java.util.Random</a> class is often preferable to <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--" rel="noreferrer">java.lang.Math.random()</a>.</p> <p>In particular, there is no need to reinvent the random integer generation wheel when there is a straightforward API within the standard library to accomplish the task.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload