Previous
Next

java util Random Class - java.util.Random Class in Java

Last updated on

java.util.Random Class

Random Class in Java

Java Random Class

The Java Random class is used to generate a stream of pseudorandom numbers. In other words Random class is used to generate random numbers in Java. The Java Random class uses a 48-bit seed, which is modified using a linear congruential formula.

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random() simpler to use.

Following Java Random class example you can learn about java.util.Random class. And also learn how to use java.util.Random class.

java.util.Random Class Example

/*  Java Random Class Example
    Save with file name RandomExample.java  */
public class RandomExample
{
  public static void main(String args[])
  {
    //java.util.Random DECLARATION
    java.util.Random r;
    //java.util.Random OBJECT CREATION
    //USING DEFAULT CONSTRUCTOR
    r = new java.util.Random();
    //java.util.Random OUTPUT
    //RETURNS NEXT RANDOM BOOLEAN
    System.out.println(r.nextBoolean());
    //RETURNS NEXT RANDOM INT
    System.out.println(r.nextInt());
    //RETURNS NEXT RANDOM INT
    System.out.println(r.nextInt(10));
    //RETURNS NEXT RANDOM FLOAT
    System.out.println(r.nextFloat());
    //RETURNS NEXT RANDOM LONG
    System.out.println(r.nextLong());
    //RETURNS NEXT RANDOM DOUBLE
    System.out.println(r.nextDouble());
    //SET THE SEED
    r.setSeed(10);
    //RETURNS NEXT RANDOM DOUBLE
    System.out.println(r.nextDouble());
  }
}
Share This Page On :

java.util.Random Class Example 2

/*  Java Random Class Example 2
    Save with file name RandomExample2.java */
public class RandomExample2
{
  public static void main(String args[])
  {
    //java.util.Random DECLARATION
    java.util.Random r;
    //java.util.Random OBJECT CREATION
    //USING PARAMETER CONSTRUCTOR
    r = new java.util.Random(100L);
    //java.util.Random OUTPUT
    //RETURNS NEXT RANDOM BOOLEAN
    System.out.println(r.nextBoolean());
    //RETURNS NEXT RANDOM INT
    System.out.println(r.nextInt());
    //RETURNS NEXT RANDOM INT
    System.out.println(r.nextInt(10));
    //RETURNS NEXT RANDOM FLOAT
    System.out.println(r.nextFloat());
    //RETURNS NEXT RANDOM LONG
    System.out.println(r.nextLong());
    //RETURNS NEXT RANDOM DOUBLE
    System.out.println(r.nextDouble());
  }
}
    

More Related Tutorials

Previous
Next