Java random number between 1 and 100
In this post, you will learn different ways to find the random number between 1 and 100 using the Java programming language.
Such a type of logical question is generally asked in an interview or in competitive exams. This can be helpful to improve your logical programming skills and is helpful in game development and other logical application development.
In this article, we have used the following in-built Java packages or classes to get the random number between 1 and 100.
- Using random.nextInt() method
- Using Math.random() method
- Using ThreadLocalRandom.current.nextInt() method
Java generate random number using random.nextInt()
The java.util.Random package provides a Random class to generate multiple types of random numbers, whether it is an int or a float. The nextInt() method of Random class is used to get a pseudorandom, uniformly distributed int value between 0 and the specified value. The given example demonstrates how to generate the random number using this method.
import java.util.*;
public class GenerateRandomNo {
public static void main(String args[])
{
// create random object
Random ran = new Random();
// get number between 0-99
int num = ran.nextInt(100);
System.out.println
("Random number between 0 and 100: " + num);
}
}
Output of the above code:
Random number between 0 and 100: 20
Java generate random number using Math.random()
The Math.random() method returns a pseudorandom number of data type double. It is a static method of the Math class. We can use this method to generate the random number between the given min and max value. Here is the syntax-
int randomNumber = (int) (Math.random()*(max-min)) + min;
In the given example, we have used the Math.random() function to generate the random number between 1 to 100.
// Generating random number
// range of 1 to 100 using Math.random
public class GenerateRandomNumbers
{
public static void main(String[] args) {
int min = 1;
int max = 100;
int randomNum = (int) (Math.random()*(max-min)) + min;
System.out.println("Random number between 0 and 100: "+randomNum);
}
}
Output of the above code:
Random number between 0 and 100: 61
Java generate random number using ThreadLocalRandom.current.nextInt()
The nextInt() method of Java ThreadLocalRandom class returns a pseudorandom int value. This method overrides the nextInt() in class Random. ThreadLocalRandom is used to generate random number typically for multi threading environment. Here is the syntax-
public int nextInt()
This method returns a pseudorandom int value between zero and the specified bound. The given example generates random number between range 1 to 100 using the ThreadLocalRandom.current.nextInt() method.
// Generating random number
// range from 1 to 100 using ThreadLocalRandom
import java.util.concurrent.ThreadLocalRandom;
public class GenerateRandomNumbers
{
public static void main(String[] args) {
int min = 1;
int max = 100;
int randomNumber = ThreadLocalRandom.current().nextInt(min, max) + min;
System.out.println("Random Numbers: " +randomNumber);
}
}
Output of the above code:
Random Numbers: 99
Related Articles
Enum from string JavaJava enum
Sort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java