Random Number Between 1 and 100 in Java

Java random number between 1 and 100

TL;DR
To generate random number between 1 and 100 in java, you can use Math.random() method.

Output:

Random number between 1 and 100: 26

Math.random Java between 1 and 100

Java provides the Math class to perform all sorts of mathematical operations.

The class also has a method named random() that produces pseudo-random numbers in uniform distribution.

The definition of the random() method is given below.

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.

Output:

Random number between 1 and 100: 6

Here is generic formula to get random number between min and max value in Java.

Let’s put min as 1 and max as 100 in the formula.

Output:

Random number between 1 and 100: 47

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, floating-point 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.

This method returns a pseudo-random 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.

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.

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 the getAsInt() method.

Let us see the example code.

Output:

Random number between 1 and 100: 71

Using Apache Commons Library to Generate Random Number Between 1 and 100 in Java

Apache Commons is a popular third-party 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.

The method generates a pseudo-random 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.

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.

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

That’s all about how to get Random Number Between 1 and 100.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *