Java Random Class



Introduction

The Java Random class instance is used to generate a stream of pseudorandom numbers.Following are the important points about Random −

  • The class uses a 48-bit seed, which is modified using a linear congruential formula.

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

Class declaration

Following is the declaration for java.util.Random class −

public class Random
   extends Object
   implements Serializable

Class constructors

Sr.No. Constructor & Description
1

Random()

This creates a new random number generator.

2

Random(long seed)

This creates a new random number generator using a single long seed.

Class methods

Sr.No. Method & Description
1 DoubleStream doubles()

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).

2 IntStream ints()

Returns an effectively unlimited stream of pseudorandom int values.

3 LongStream longs()

Returns an effectively unlimited stream of pseudorandom long values.

4 boolean nextBoolean()

This method returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.

5 void nextBytes(byte[] bytes)

This method generates random bytes and places them into a user-supplied byte array.

6 double nextDouble()

This method returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

7 float nextFloat()

This method returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

8 double nextGaussian()

This method returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

9 int nextInt()

This method returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

10 long nextLong()

This method returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

11 void setSeed(long seed)

This method sets the seed of this random number generator using a single long seed.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Getting a Random Double Number Example

The following example shows the usage of Java Random doubles() method. Firstly, we've created a Random object and then using doubles() we retrieved a stream of random double values and printed first value.

package com.tutorialspoint;

import java.util.Random;
import java.util.stream.DoubleStream;

public class RandomDemo {
   public static void main( String args[] ) {

      // create random object
      DoubleStream randomNoStream = new Random().doubles();
      // print a random double value
      randomNoStream.limit(1).forEach( i -> System.out.println(i));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

0.5129590222446587
Advertisements