Home Java Example Java Util Generate random numbers in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Generate random numbers in Java

Advertisement
In this tutorial we will use the java.util.Random class and then write code to generate 10 random number between 0 and 1000.

Generate random numbers in Java - How to use the java.util.Random class to generate desired random number in Java program?

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:

Generate random numbers in Java

Check more Tutorials

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 22, 2013

Related Tutorials

Discuss: Generate random numbers in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
yuvaraj
December 22, 2013
what is the use of "Final" key word

with out the use of final key word do the program run ??
DMCA.com