How to generate random number in java

This section shows you how to create a random number and example provided here will help you easily understand and use the code to generate random numbers in your java application.


 

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

How to generate random number in java

In this section you will learn how to generate random number in java. Java API  provide a random class in java.util.Random package which generate random number within a range. The random() method is most convenient way to generate random number in java which return only double value .You can also generate pseudo-random floating or double type numbers.

Random Numbers in Java with Example

As we discussed that to create a random number, java provide a random() method in java.util.Random class. You have to create the instance of random class. This class provide method to return random integer, double and float. The following example will illustrate the random number generation in java.

import java.util.Random;
public class RandomNumber 
{
 public static void main(String args[])
 {
   Random random = new Random();
   for(int i =0; i<5; i++){
   int randomInteger = random.nextInt();
   System.out.println("Random Integer in Java: " + randomInteger);
  }
 }
}

As this program execute it will generate five random numbers. If you are using java Math.random  it will generate only double value not integer. Some of the methods of Random class are as follows :

  • next(int num) - Generate next pseudorandom number.
  • nextBoolean() - Returns the next pseudorandom, boolean values.
  • nextInt() -  Returns all pseudorandom integer type in equal probability.
  • nextDouble() - Similar to integer it returns in double with range 0.0 to 1.0.
  • nextFloat() - Returns pseudorandom float values with range from 0.0 to 1.0.
  • nextLong() - Returns pseudorandom long value in the sequence.

So,  if you will compile and execute the program then you will  get the output as follows:

Download Source Code

Advertisements

Share on Google+Share on Google+

How to generate random number in java

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

Advertisements

Ads
 

Discuss: How to generate random number in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads