Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Generate random numbers in Java

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

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Advertisements

Share on Google+Share on Google+

Generate random numbers in Java

Posted on: December 22, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

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 ??

Ads

 

Ads