random class java

Let’s learn random class in java.

random class java

An instance of Random class is used to generate a stream of pseudorandom numbers. Instances of java.util.Random are not cryptographically secure.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance.

random class declaration

public class Random extends Object implements Serializable

random class constructors

public Random() – creates a new random number generator.

public Random(long seed) – creates a new random number generator using a single long seed.

random methods

Here are few random methods java,

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

ints() – returns an effectively unlimited stream of pseudorandom int values.

longs() – returns an effectively unlimited stream of pseudorandom long values.

next(int bits) – generates the next pseudorandom number.

nextBoolean() – returns the next pseudorandom, uniformly distributed boolean value from this random number generator’s sequence.

nextBytes(byte[] bytes) – generates random bytes and places them into a user-supplied byte array.

nextDouble() – returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence.

nextFloat() – returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator’s sequence.

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.

nextInt() – java nextint returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequence.

nextInt(int bound) – Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence.

nextLong() – returns the next pseudorandom, uniformly distributed long value from this random number generator’s sequence.

setSeed(long seed) – Sets the seed of this random number generator using a single long seed.


Let’s see random class java example.

import java.util.Random;
public class JavaRandomClassExample
{
   public static void main(String[] args)
   {
      Random rand = new Random();
      System.out.println(rand.nextInt(10));
      System.out.println(rand.nextBoolean());
      System.out.println(rand.nextDouble());
      System.out.println(rand.nextFloat());
      System.out.println(rand.nextGaussian());
      byte[] bytes = new byte[10];
      rand.nextBytes(bytes);
      System.out.printf("[");
      for(int a = 0; a < bytes.length; a++)
      {
         System.out.printf("%d ", bytes[a]);
      }
      System.out.printf("]\n");
      System.out.println(rand.nextLong());  
      System.out.println(rand.nextInt());
      long seed = 55;
      rand.setSeed(seed);
   }
}


Output:

6
true
0.2805854768046321
0.13993335
0.32107779397797304
[-114 29 112 38 -67 -67 81 -58 -18 51 ]
9211389898189124398
-1318125037


Reference – oracle docs

Also read – inheritance in java