Java random number between 1 and 10
In this post, you will learn different ways to find a random number between 1 and 10 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 10.
- Using the random.nextInt() method
- Using the Math.random() method
- Using the 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 they are ints or floats. The nextInt() method of this 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-9
int nxt = ran.nextInt(10);
System.out.println
("Random number between 0 and 10 : " + nxt);
}
}
Output of the above code:
Random number between 0 and 10 is : 4
Suppose there is a requirement to generate a random number 5 times using the nextInt() method. The code will be as follows.
import java.util.*;
public class GenerateRandomNo {
public static void main(String[] args) {
Random random = new Random();
for(int i = 1; i <=5; i++) {
int value = random.nextInt((10 - 1) + 1) + 1;
System.out.println(value);
}
}
}
Output of the above code:
9
2
7
4
9
Java generate random number using Math.random()
We can also use the Math.random() method to generate the random number between the given min and max values. 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 10.
// Generating 10 random numbers
// range of 1 to 10 using Math.random
public class GenerateRandomNumbers
{
public static void main(String[] args) {
int min = 1;
int max = 10;
System.out.println("Ten Random Numbers: ");
for(int i = 0; i < 10; i++) {
int randomNum = (int) (Math.random()*(max-min)) + min;
System.out.println(randomNum);
}
}
}
Output of the above code:
Ten Random Numbers:
7
4
5
1
9
7
6
3
7
7
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. Here is the syntax-
public int nextInt()
The given example generates 7 random numbers between range 1 to 10 using the ThreadLocalRandom.current.nextInt() method-
// Generating 10 random numbers
// range of 1 to 10 using ThreadLocalRandom
import java.util.concurrent.ThreadLocalRandom;
public class GenerateRandomNumbers
{
public static void main(String[] args) {
int min = 1;
int max = 10;
System.out.println("Random Numbers: ");
for(int i = 0; i < 7; i++) {
int randomNumber = ThreadLocalRandom.current().nextInt(min, max) + min;
System.out.println(randomNumber);
}
}
}
Output of the above code:
Random Numbers:
4
6
7
5
10
8
2
Related Articles
Number pattern programs in JavaJava 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
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number