Java.util.Random class in Java

07 May, 2019
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:
Random(): Creates a new random number generator
Random(long seed): Creates a new random number generator using a single long seed
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 valuesjava.util.Random.ints(): Returns an effectively unlimited stream of pseudo random int valuesSyntax:public IntStream ints()
Returns:
a stream of pseudorandom int valuesjava.util.Random.longs(): Returns an effectively unlimited stream of pseudo random long valuesSyntax:public LongStream longs()
Returns:
a stream of pseudorandom long valuesnext(int bits): java.util.Random.next(int bits) Generates the next pseudo random numberSyntax:protected int next(int bits)
Parameters:
bits - random bits
Returns:
the next pseudo random value from this 
random number generator's sequencejava.util.Random.nextBoolean(): Returns the next pseudo random, uniformly distributed boolean value from this random number generator’s sequenceSyntax:public boolean nextBoolean()
Returns:
the next pseudorandom, uniformly distributed boolean value 
from this random number generator's sequencejava.util.Random.nextBytes(byte[] bytes) :Generates random bytes and places them into a user-supplied byte arraySyntax:public void nextBytes(byte[] bytes)
Parameters:
bytes - the byte array to fill with random bytes
Throws:
NullPointerException - if the byte array is nulljava.util.Random.nextDouble(): Returns the next pseudo random, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequenceSyntax:public double nextDouble()
Returns:
the next pseudo random, uniformly distributed double 
value between 0.0 and 1.0 from this 
random number generator's sequencejava.util.Random.nextFloat(): Returns the next pseudo random, uniformly distributed float value between 0.0 and 1.0 from this random number generator’s sequenceSyntax:public float nextFloat()
Returns:
the next pseudorandom, uniformly distributed float value 
between 0.0 and 1.0 from this 
random number generator's sequencejava.util.Random.nextGaussian(): Returns the next pseudo random, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator’s sequenceSyntax: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 sequencejava.util.Random.nextInt(): Returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequenceSyntax:public int nextInt()
Returns:
the next pseudorandom, uniformly distributed int value from 
this random number generator's sequencejava.util.Random.nextInt(int bound): Returns a pseudo random, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequenceSyntax: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 positivejava.util.Random.nextLong(): Returns the next pseudorandom, uniformly distributed long value from this random number generator’s sequenceSyntax:public long nextLong()
Returns:
the next pseudorandom, uniformly distributed long value
from this random number 
generator's sequencejava.util.Random.setSeed(long seed): Sets the seed of this random number generator using a single long seedSyntax:public void setSeed(long seed)
Parameters:
seed - the initial seed
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 DoubleStream doubles()
Returns:
a stream of pseudorandom double values
java.util.Random.ints(): Returns an effectively unlimited stream of pseudo random int valuesSyntax:public IntStream ints()
Returns:
a stream of pseudorandom int values
public IntStream ints()
Returns:
a stream of pseudorandom int values
java.util.Random.longs(): Returns an effectively unlimited stream of pseudo random long valuesSyntax:public LongStream longs()
Returns:
a stream of pseudorandom long values
public LongStream longs()
Returns:
a stream of pseudorandom long values
next(int bits): java.util.Random.next(int bits) Generates the next pseudo random numberSyntax:protected int next(int bits)
Parameters:
bits - random bits
Returns:
the next pseudo random value from this 
random number generator's sequence
protected int next(int bits)
Parameters:
bits - random bits
Returns:
the next pseudo random value from this 
random number generator's sequence
java.util.Random.nextBoolean(): Returns the next pseudo random, uniformly distributed boolean value from this random number generator’s sequenceSyntax:public boolean nextBoolean()
Returns:
the next pseudorandom, uniformly distributed boolean 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
java.util.Random.nextBytes(byte[] bytes) :Generates random bytes and places them into a user-supplied byte arraySyntax:public void nextBytes(byte[] bytes)
Parameters:
bytes - the byte array to fill with random bytes
Throws:
NullPointerException - if the byte array is null
public void nextBytes(byte[] bytes)
Parameters:
bytes - the byte array to fill with random bytes
Throws:
NullPointerException - if the byte array is null
java.util.Random.nextDouble(): Returns the next pseudo random, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequenceSyntax: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 double nextDouble()
Returns:
the next pseudo random, uniformly distributed double 
value between 0.0 and 1.0 from this 
random number generator's sequence
java.util.Random.nextFloat(): Returns the next pseudo random, uniformly distributed float value between 0.0 and 1.0 from this random number generator’s sequenceSyntax: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 float nextFloat()
Returns:
the next pseudorandom, uniformly distributed float value 
between 0.0 and 1.0 from this 
random number generator's sequence
java.util.Random.nextGaussian(): Returns the next pseudo random, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator’s sequenceSyntax: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 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
java.util.Random.nextInt(): Returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequenceSyntax:public int nextInt()
Returns:
the next pseudorandom, uniformly distributed int value 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
java.util.Random.nextInt(int bound): Returns a pseudo random, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequenceSyntax: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 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
java.util.Random.nextLong(): Returns the next pseudorandom, uniformly distributed long value from this random number generator’s sequenceSyntax:public long nextLong()
Returns:
the next pseudorandom, uniformly distributed long value
from this random number 
generator's sequence
public long nextLong()
Returns:
the next pseudorandom, uniformly distributed long value
from this random number 
generator's sequence
java.util.Random.setSeed(long seed): Sets the seed of this random number generator using a single long seedSyntax:public void setSeed(long seed)
Parameters:
seed - the initial seed
public void setSeed(long seed)
Parameters:
seed - the initial seed
Methods inherited from class java.lang.Object
clone
equals
finalize
getClass
hashCode
notify
notifyAll
toString
wait
Java program to demonstrate usage of Random class
// Java program to demonstrate// method calls of Random classimport java.util.Random;  public class Test{    public static void main(String[] args)    {        Random random = new Random();        System.out.println(random.nextInt(10));        System.out.println(random.nextBoolean());        System.out.println(random.nextDouble());        System.out.println(random.nextFloat());        System.out.println(random.nextGaussian());        byte[] bytes = new byte[10];        random.nextBytes(bytes);        System.out.printf("[");        for(int i = 0; i< bytes.length; i++)        {            System.out.printf("%d ", bytes[i]);        }        System.out.printf("]\n");                    System.out.println(random.nextLong());        System.out.println(random.nextInt());             long seed = 95;      random.setSeed(seed);              // Note: Running any of the code lines below      // will keep the program running as each of the       // methods below produce an unlimited random       // values of the corresponding type        /* System.out.println("Sum of all the elements in the IntStream returned = " +         random.ints().count());      System.out.println("Count of all the elements in the DoubleStream returned = " +         random.doubles().count());      System.out.println("Count of all the elements in the LongStream returned = " +         random.longs().count());             */       }}     
Output:
4
true
0.19674934340402916
0.7372021
1.4877581394085997
[-44 75 68 89 81 -72 -1 -66 -64 117 ]
158739962004803677
-1344764816

Reference:
Oracle
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Akanksha_Rai
Java - util package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java