. Advertisement .
..3..
. Advertisement .
..4..
In this post, we will consider some methods for executing Java random number generation from 1 to 10. There are 3 Java classes or packages presented below for conducting this functionality. Keep on reading to select the suitable method to use.
How to Generate a Random Number Between 1 and 10 in Java
1. Execute Java Random Number Generation From 1 to 10 by Using random.nextInt()
You can use a random class for generating Java pseudo-random numbers. Thread-safe is known as a version of this class, however, it is cryptographically unsafe. There are various method calls provided by this class for generating different types of data such as int, double, and float.
According, java.util.Random.nextInt() is one of the methods contained in the Random class. The next pseudorandom will be returned by this method and distributed uniformly from the sequence of generating a random number.
import java.util.Random;
public class Main {
public static void main(String[] args) {
int min = 1;
int max = 10;
Random random = new Random();
int value = random.nextInt(max + min) + min;
System.out.println(value);
}
}
Output:
6
To demonstrate that we can execute Java random number generation from 1 to 10 by using the above method, a loop can be used for generating a number till it ends. The reason for that is there is not a large number range and we can repeat those random numbers.
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
for(int i = 1; i <=10; i++) {
int value = random.nextInt((10 - 1) + 1) + 1;
System.out.println(value);
}
}
Output:
10
7
2
9
2
7
6
4
9
Notably, if your demand is more complex, for instance, you need to generate a random number in a range or you need to execute simultaneous random numbers generation among multiple threads, then you are suggested to use other available random methods in Java.
2. Execute Java Random Number Generation From 1 to 10 by Using Math.random()
The function Math.random()
will return a float-point which is also known as a pseudo-random number from 0 to lower than 1 with estimated uniform distribution in that range. By using this function, you can optionally scale the range. Selecting the initial seeds will be decided by the implementation but not the user.
public class Main {
public static void main(String[] args) {
int min = 1;
int max = 10;
for(int i = min; i <=max; i++) {
int getRandomValue = (int) (Math.random()*(max-min)) + min;
System.out.println(getRandomValue);
}
}
Output:
5
5
2
1
6
9
3
6
5
7
As the method Math.random() is synchronized properly to commit that the return value is correct when being used by multiple threads, it becomes a limitation when those threads use it simultaneously. Hence, the next section would introduce you to a more efficient way to execute Java random number generation from 1 to 10.
3. Execute Java Random Number Generation From 1 to 10 by Using ThreadLocalRandom.current.nextInt()
ThreadLocalRandom is a subclass of the class Random as mentioned in the first section. This method is used for random numbers generation which is isolated from the present thread. The ThreadLocalRandom initialization is done by using a seed value generated internally since there is no other modification allowed.
Though as known that JDK 7 introduced this method for the program multi-thread, it reveals the limitation of cryptographically unsafety.
In the below example, the method current()
have to be called since we expect a random number generation in the present thread.
import java.util.concurrent.ThreadLocalRandom;
public class Main {
public static void main(String[] args) {
int min = 1;
int max = 10;
for(int i = 1; i <=10; i++) {
int getRandomValue = ThreadLocalRandom.current().nextInt(min, max) + min;
System.out.println(getRandomValue);
}
}
}
Output:
3
4
5
8
6
2
6
10
6
2
Conclusion
This blog has provided you with detailed tutorials on Java random number generation from 1 to 10. Also, the pros and cons of each method have been explained to you for better application. If you want to explore more ways to work on Java, visit other posts on our website for more details!
Leave a comment