Welcome to AHT TECH JSC!

# Java random number between 1 and 10: How to generate it

In case you are an app or website developer. So you must have known Java. Therefore, in this blog today, ArrowHiTheme will bring to you some interesting information about Java. It is a random number between 1 and 10 Java: How to generate it. Are you ready? Can we start right now?

## Using random.nextInt() to generate random number between 1 and 10 Java

Firstly, java.util.Random is a Java package that may be used to produce a random number from a range of values. Additionally, the range in our example.

Secondly, Random is a class in this package that lets us produce multiple kinds of numbers, including ints and floats. Therefore, for a better understanding, look at the example.

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

Therefore, we may use a loop to generate a random number until it finishes to prove that the previous technique works and generates random numbers every time. Additionally, the random number between 1 and 10 java may be repeated. Because we do not have a big range of 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:

## Using Math.random() to generate random number between 1 and 10 Java

You can also use the Math.random() method to generate a android random number between 1 and 10 using the Math.random() method. So you can use Math. ran to iterate from min to max

Therefore here’s its formula:

int randomNumber = (int) (Math.random()*(max-min)) + min;

For example

Output:

You can use this method to generate a random number in the current thread:

• To generate it, use ThreadLocalRandom.current.nextInt().

For example:

Output:

## Conclusion

To conclude that, we have already provided you information about How to generate a random number between 1 and 10 Java. However, if you have any questions about it or any information about Mobile App Development.. So you can CONTACT US right now. We are a leading web application development company in the world. We provide services personalized in accordance with your business needs

Share