[ad_1]
Oftentimes, as a Java programmer, you might need to generate random numbers in a particular range.
Java provides several inbuilt libraries to perform this task. There are thirdparty libraries as well to generate the random numbers.
This article discusses the following methods to generate random number between 1 and 100.
 Random class of Java Utility library.
 The random() method of Java Math class.
 Apache Commons library’ RandomUtils class
 Generating random numbers while working with threads.
Using Java Util’s Random Class to Generate Random Number Between 1 and 100 in Java
The Random
class of Java’s Utility library provides different methods to generate random numbers.
It provides methods to generate integer numbers, floatingpoint numbers, double numbers, and random sequences of bytes.
There are different polymorphic forms of methods that can generate an integer number within a given range as well as within the range of the integer data type.
However, this article focuses on generating number between 1 and 100.
You can also provide a seed value to the Random class using the constructor or the setSeed()
method.
There are two methods to perform the task of generating the random integer between 1 and 100. Let us see each of them one by one.
The nextint() Method
The definition of the nextInt()
method is given below.
public int nextInt(int bound)

This method returns a pseudorandom number between 0 and the ‘bound’ parameter. Note that 0 is inclusive and the ‘bound’ is exclusive.
To generate a number between 1 and 100, both inclusive, you can follow the steps given below.
 Create an instance of the Random class.
 Generate a random number by calling the
nextInt()
method and passing the upper bound (100) to the method as a parameter.  It will generate a number between 0 (inclusive) and 100 (exclusive).
 To make it between 1(inclusive) and 100 (inclusive), add 1 to the number returned by the method.
Let us see the code implementing the above approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
package java2blog;
import java.util.Random;
public class RandomIntegerExample {
public static void main(String[] args) { Random randI = new Random(); int myRandInt = randI.nextInt(100); myRandInt = myRandInt+1; System.out.println(“Random number between 1 and 100: “+myRandInt); }
}

Output:
Random number between 1 and 100: 87
The ints() Method
The ints()
method is also defined in the Random class. It has several different polymorphic forms.
You can use this method to generate a stream of random numbers in a range. The ints()
method is defined as given below.
public IntStream ints(int randomNumberOrigin, int randomNumberBound)

You can note that this method returns a stream of integers rather than a single integer. You can retrieve the integers from the stream.
Let us see the steps to generate a random number using the ints()
method.
 Create an instance of the Random class.
 Invoke the
ints()
method by passing 1 and 101 as the parameters.  This will generate the numbers in the range from 1(inclusive) and 101(exclusive). It means you will get numbers between 1 and 100.
 Extract any random number from all the numbers by calling the
findAny()
and thegetAsInt()
method.
Let us see the example code.
package java2blog;
import java.util.Random;
public class RandomIntegerExample {
public static void main(String[] args) { Random randI = new Random(); int myRandInt = randI.ints(1, 101).findAny().getAsInt(); System.out.println(“Random number between 1 and 100: “+myRandInt); }
}

Output:
Random number between 1 and 100: 71
Using Math Class to to Generate Random Number Between 1 and 100 in Java
Java provides the Math class to perform all sorts of mathematical operations.
The class also has a method named random()
that produces pseudorandom numbers in uniform distribution.
The definition of the random()
method is given below.
public static double random()

The random() method generates a double number between 0.0 (inclusive) and 1.0 (exclusive).
You can generate the number between 1 and 100 using the Math.random()
method by following the steps given below.
 Generate a random number by invoking the
Math.random()
method.  Multiply the number with 100.
 Add 1 to the number.
 If you want the integer result, explicitly cast the result to ‘int’.
Let us see the code.
package java2blog;
public class RandomIntegerExample {
public static void main(String[] args) { double num = Math.random(); int myRandInt = (int)(num*100+1);
System.out.println(“Random number between 1 and 100: “+myRandInt); }
}

Output:
Random number between 1 and 100: 6
Using Apache Commons Library to Generate Random Number Between 1 and 100 in Java
Apache Commons is a popular thirdparty library in Java developed by Apache.
The library defines a RandomUtils class that contains several methods to produce random numbers of different data types.
The RandomUtils
class contains two polymorphic forms of the nextInt()
method that you can use to generate random integers.
One of these forms generates the numbers between a given range. Let us see the definition of the nextInt()
method.
public static int nextInt(int startInclusive, int endExclusive)

The method generates a pseudorandom number between the first argument (inclusive) and the second argument (exclusive).
Note that this method throws the IllegalArgumentsException
if you provide negative arguments of if the startInclusive
is greater than the endExclusive
argument.
To generate a number between 1 and 100,
 Invoke the
nextInt()
method by passing 0 and 100 as parameters.  Add 1 to the result returned by the method.
Note that you do not need an instance of the class as the method is a static method.
Let us see the code.
package java2blog;
import org.apache.commons.lang3.RandomUtils;
public class RandomIntegerExample {
public static void main(String[] args) { int myRandInt = RandomUtils.nextInt(0, 100); myRandInt = myRandInt+1; System.out.println(“Random number between 1 and 100: “+myRandInt); } }

Output:
Random number between 1 and 100: 95
Note that you can alternatively pass 1 and 101 as parameters to the method. It will generate the same results.
Using ThreadLocalRandom Library to Generate Random Number Between 1 and 100 in Java in a Concurrent Environment
Although we can use Java Utility’s Random class as well as Math class to generate random numbers in threads, they can degrade the performance.
It is therefore recommended to use the ThreadLocalRandom to generate the random numbers while working with threads.
The ThreadLocalRandom class is a subclass of Java Utility’s Random class. You can use the nextInt()
method of this subclass to generate the random integer number in a thread.
You can pass two parameters to the nextInt()
method. The number is generated between the first parameter (inclusive) and the second parameter (exclusive).
You can follow the given steps to generate a random number between 1 and 100.
 Invoke the static
current()
method of the ThreadLocalRandom class using the class name. It returns the ThreadLocalRandom of the current thread.  Invoke the
nextInt()
method with 1 and 101 as parameters.  It will generate a random number between 1 (inclusive) and 100 (inclusive).
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
package java2blog;
import java.util.concurrent.ThreadLocalRandom;
public class RandomIntegerExample {
public static void main(String[] args) { int myRandInt = ThreadLocalRandom.current().nextInt(1, 101);
System.out.println(“Random number between 1 and 100: “+myRandInt);
}
}

Output:
Random number between 1 and 100: 20
Conclusion
This article has discussed four different ways to generate the random number between 1 and 100.
You can generate the random number between any given range of numbers by simply varying the parameters to the methods as discussed in the article.
You can also produce numbers with other data types using similar methods.
Hope you enjoyed reading the article. Stay tuned for more articles. Happy learning!
References
[ad_2]