Java.util.Random class in Java

Random class is used to generate pseudo-random numbers in java. An instance of this class is thread-safe. The instance of this class is however cryptographically insecure. This class provides various method calls to generate different random data types such as float, double, int.

Constructors:

Declaration:

public class Random extends Object implements Serializable

Methods:

    java.util.Random.doubles(): Returns an effectively unlimited stream of pseudo random double values, each between zero (inclusive) and one (exclusive)
    Syntax:

public DoubleStream doubles() Returns: a stream of pseudorandom double values
public IntStream ints() Returns: a stream of pseudorandom int values
public LongStream longs() Returns: a stream of pseudorandom long values
protected int next(int bits) Parameters: bits - random bits Returns: the next pseudo random value from this random number generator's sequence
public boolean nextBoolean() Returns: the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence
public void nextBytes(byte[] bytes) Parameters: bytes - the byte array to fill with random bytes Throws: NullPointerException - if the byte array is null
public double nextDouble() Returns: the next pseudo random, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence
public float nextFloat() Returns: the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence
public double nextGaussian() 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
public int nextInt() Returns: the next pseudorandom, uniformly distributed int value from this random number generator's sequence
public int nextInt(int bound) Parameters: bound - the upper bound (exclusive). Must be positive. Returns: the next pseudorandom, uniformly distributed int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence Throws: IllegalArgumentException - if bound is not positive
public long nextLong() Returns: the next pseudorandom, uniformly distributed long value from this random number generator's sequence
public void setSeed(long seed) Parameters: seed - the initial seed

Methods inherited from class java.lang.Object

Java program to demonstrate usage of Random class