• In programming world, we often need to generate random numbers, sometimes random integers in a range e.g. 1 to 100 etc.
  • Random number generation in Java is easy as Java API provides good support for random numbers via java.util.Random class, Math.random() utility method and recently ThreadLocalRandom class in Java 7.
  • random() method is the most convenient way of generating randoms in Java it only returns random doubles, on the other hand by using Random, we can generate pseudo-random integer, floating point numbers e.g. double and even random boolean values.

Three ways to generate random integers in a range.

  1. java.util.Random.nextInt
  2. Math.random.
  3. java.util.Random.ints (Java 8)

java.util.Random

  • This Random().nextInt(int bound) generates a random integer from 0 (inclusive) to bound (exclusive).

For getRandomNumberInRange(5, 10), this will generates a random integer between 5 (inclusive) and 10 (inclusive).

[pastacode lang=”java” manual=”private%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0A%0A%09if%20(min%20%3E%3D%20max)%20%0A%7B%0A%09%09throw%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0A%0A%09Random%20r%20%3D%20new%20Random()%3B%0A%09return%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A” message=”java code” highlight=”” provider=”manual”/]

What is (max – min) + 1) + min?

Above formula will generates a random integer in a range between min (inclusive) and max (inclusive).

//1. nextInt(range) = nextInt(max – min)

[pastacode lang=”java” manual=”%2F%2FRandom().nextInt(int%20bound)%20%3D%20Random%20integer%20from%200%20(inclusive)%20to%20bound%20(exclusive)%0A%0A%0Anew%20Random().nextInt(5)%3B%20%20%2F%2F%20%5B0…4%5D%20%5Bmin%20%3D%200%2C%20max%20%3D%204%5D%0Anew%20Random().nextInt(6)%3B%20%20%2F%2F%20%5B0…5%5D%0Anew%20Random().nextInt(7)%3B%20%20%2F%2F%20%5B0…6%5D%0Anew%20Random().nextInt(8)%3B%20%20%2F%2F%20%5B0…7%5D%0Anew%20Random().nextInt(9)%3B%20%20%2F%2F%20%5B0…8%5D%0Anew%20Random().nextInt(10)%3B%20%2F%2F%20%5B0…9%5D%0Anew%20Random().nextInt(11)%3B%20%2F%2F%20%5B0…10%5D%0A” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

//2. To include the last value (max value) = (range + 1)

[pastacode lang=”java” manual=”new%20Random().nextInt(5%20%2B%201)%20%20%2F%2F%20%5B0…5%5D%20%5Bmin%20%3D%200%2C%20max%20%3D%205%5D%0Anew%20Random().nextInt(6%20%2B%201)%20%20%2F%2F%20%5B0…6%5D%0Anew%20Random().nextInt(7%20%2B%201)%20%20%2F%2F%20%5B0…7%5D%0Anew%20Random().nextInt(8%20%2B%201)%20%20%2F%2F%20%5B0…8%5D%0Anew%20Random().nextInt(9%20%2B%201)%20%20%2F%2F%20%5B0…9%5D%0Anew%20Random().nextInt(10%20%2B%201)%20%2F%2F%20%5B0…10%5D%0Anew%20Random().nextInt(11%20%2B%201)%20%2F%2F%20%5B0…11%5D%0A” message=”java code” highlight=”” provider=”manual”/]

//3. To define a start value (min value) in a range,

//   For example, the range should start from 10 = (range + 1) + min

[pastacode lang=”java” manual=”new%20Random().nextInt(5%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0…5%5D%20%20%2B%2010%20%3D%20%5B10…15%5D%0Anew%20Random().nextInt(6%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0…6%5D%20%20%2B%2010%20%3D%20%5B10…16%5D%0Anew%20Random().nextInt(7%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0…7%5D%20%20%2B%2010%20%3D%20%5B10…17%5D%0Anew%20Random().nextInt(8%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0…8%5D%20%20%2B%2010%20%3D%20%5B10…18%5D%0Anew%20Random().nextInt(9%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0…9%5D%20%20%2B%2010%20%3D%20%5B10…19%5D%0Anew%20Random().nextInt(10%20%2B%201)%20%2B%2010%20%2F%2F%20%5B0…10%5D%20%2B%2010%20%3D%20%5B10…20%5D%0Anew%20Random().nextInt(11%20%2B%201)%20%2B%2010%20%2F%2F%20%5B0…11%5D%20%2B%2010%20%3D%20%5B10…21%5D%0A” message=”java code” highlight=”” provider=”manual”/]

// Range = (max – min)

// So, the final formula is ((max – min) + 1) + min

 

//4. Test [10…30]

// min = 10 , max = 30, range = (max – min)

[pastacode lang=”java” manual=”new%20Random().nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%0Anew%20Random().nextInt((30%20-%2010)%20%2B%201)%20%2B%2010%0Anew%20Random().nextInt((20)%20%2B%201)%20%2B%2010%0Anew%20Random().nextInt(21)%20%2B%2010%20%20%20%20%2F%2F%5B0…20%5D%20%2B%2010%20%3D%20%5B10…30%5D%0A” message=”java code” highlight=”” provider=”manual”/]

//5. Test [15…99]

// min = 15 , max = 99, range = (max – min)

[pastacode lang=”java” manual=”new%20Random().nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%0Anew%20Random().nextInt((99%20-%2015)%20%2B%201)%20%2B%2015%0Anew%20Random().nextInt((84)%20%2B%201)%20%2B%2015%0Anew%20Random().nextInt(85)%20%2B%2015%20%20%20%20%2F%2F%5B0…84%5D%20%2B%2015%20%3D%20%5B15…99%5D%0A” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

Example:

To generate 10 random integers in a range between 5 (inclusive) and 10 (inclusive).

WikitechyRandom.java

[pastacode lang=”java” manual=”package%20com.wiki.example.test%3B%0Aimport%20java.util.Random%3B%0Apublic%20class%20WikitechyRandom%20%0A%7B%0A%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0Afor%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%0A%7B%0ASystem.out.println(getRandomNumberInRange(5%2C%2010))%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%0A%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0A%0A%0A” message=”java code” highlight=”” provider=”manual”/]

Output.

  • 7
  • 6
  • 10
  • 8
  • 9
  • 5
  • 7
  • 10
  • 8
  • 5

Math.random

  • This Math.random() gives a random double from 0.0 (inclusive) to 1.0 (exclusive).

Formula:

int)(Math.random() * ((max – min) + 1)) + min

Example:

  • To generate 10 random integers in a range between 26 (inclusive) and 30 (inclusive).

WikitechyRandom.java

[pastacode lang=”java” manual=”package%20com.wiki.example.test%3B%0Apublic%20class%20WikitechyRandom%20%0A%7B%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0A%0A%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%0A%7B%0A%09%09%09System.out.println(getRandomNumberInRange(26%2C%2030))%3B%0A%7D%0A%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0A%0A%09%09if%20(min%20%3E%3D%20max)%20%0A%7B%0A%09%09%09throw%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0A%0A%09%09return%20(int)(Math.random()%20*%20((max%20-%20min)%20%2B%201))%20%2B%20min%3B%0A%7D%0A%0A%7D%0A” message=”java code” highlight=”” provider=”manual”/] [ad type=”banner”]

Output.

  • 27
  • 26
  • 30
  • 29
  • 30
  • 30
  • 30
  • 27
  • 30
  • 26

Note:

  • The Random.nextInt(n) is more efficient than Math.random() * n

java.util.Random.ints

  • In Java 8, New methods are added in java.util.Random

public IntStream ints(int randomNumberOrigin, int randomNumberBound)

public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

  • This Random.ints(int origin, int bound) or Random.ints(int min, int max) generates a random integer from origin (inclusive) to bound (exclusive).
[pastacode lang=”java” manual=”private%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0A%0A%09Random%20r%20%3D%20new%20Random()%3B%0A%09return%20r.ints(min%2C%20(max%20%2B%201)).findFirst().getAsInt()%3B%0A%0A%7D%0A” message=”java code” highlight=”” provider=”manual”/]

Example :

  • To generate 10 random integers in a range between 43 (inclusive) and 48 (inclusive).

WikitechyRandom.java

[pastacode lang=”java” manual=”package%20com.wiki.form.test%3B%0A%0Aimport%20java.util.Random%3B%0Apublic%20class%20WikitechyRandom%20%0A%7B%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0Afor%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%0A%7B%0ASystem.out.println(getRandomNumberInRange(43%2C%2048))%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.ints(min%2C%20(max%20%2B%201)).limit(1).findFirst().getAsInt()%3B%0A%7D%0A%7D%0A%0A%0A” message=”java code” highlight=”” provider=”manual”/]

Output.

  • 44
  • 45
  • 47
  • 43
  • 48
  • 47
  • 44
  • 45
  • 46
  • 47
[ad type=”banner”]

Categorized in: