In this tutorial we will use the java.util.Random class and then write code to generate 10 random number between 0 and 1000.
In this tutorial we will use the java.util.Random class and then write code to generate 10 random number between 0 and 1000.In this tutorial I will teach you how to write Java code using the java.util.Random class to generate single or a set of random numbers in your program.
We are using the java.util.Random class for generating the random number.
We are using the randomGenerator.nextInt(num) method to generate the random number. The java.util.Random class is used to generate the random integers, doubles, int etc..
Following code can be used to generate a random number between 0,1000:
int randomInt = randomGenerator.nextInt(1000);
Following is the code to generate 10 random number between 0 and 1000:
import java.util.Random; /** Example of Random Number generation in Java*/ public final class RandomGenerator { public static final void main(String... aArgs){ Random randomGen = new Random(); for (int idx = 1; idx <= 10; ++idx){ int randomInt = randomGen.nextInt(1000); System.out.println("Generated Number: " + randomInt); } } }
If you run the above code following output will be displayed on the browser:
Check more Tutorials
Ads