Menu
Topics Index
...
`


More Utility Classes >
Siva Nookala - 01 Apr 2016
java.util.Random instance is used to generate a stream of pseudo-random numbers.

Random Constructors:
ConstructorDescription
Random()Creates a new random number generator.
Random(long seed)Creates a new random number generator using a single long seed.

Random Methods:
MethodDescription
protected int next(int bits)Generates the next pseudorandom number.
boolean nextBoolean()Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
void nextBytes(byte[] bytes)Generates random bytes and places them into a user-supplied byte array.
double nextDouble()Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
float nextFloat()Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
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.
int nextInt()Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
int nextInt(int n)Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value n (exclusive), drawn from this random number generator's sequence.
long nextLong()Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
void setSeed(long seed)Sets the seed of this random number generator using a single long seed.

Random
import java.util.Random;

class RandomTest
{
    public static void main(String arg[])
    {
        Random r = new Random();
        
        System.out.println("The next Integer value : " + r.nextInt()); // LINE A
        System.out.println("The next Boolean value : " + r.nextBoolean());
        
        byte[] b = new byte[30];
        r.nextBytes(b); // Puts the next byte in array
        System.out.println("Value of Byte array : " + b);
        
        System.out.println("The next Double value : " + r.nextDouble());
        System.out.println("The next Float value : " + r.nextFloat());
        System.out.println("The next Gaussian value : " + r.nextGaussian());
        System.out.println("The next Integer  value between 0 to 4 : " + r.nextInt(5));
        System.out.println("The next Long value : " + r.nextLong());
        r.setSeed(20);
        System.out.println("The set Seed value : " + r.nextInt());
    
    }
}
OUTPUT

The next Integer value : 1129872169
The next Boolean value : true
Value of Byte array : [B@103074e
The next Double value : 0.47766930003831065
The next Float value : 0.13917398
The next Gaussian value : 0.8715938816913958
The next Integer  value between 0 to 4 : 1
The next Long value : 3604397887200514771
The set Seed value : -1150867590

DESCRIPTION

In this program, a random object is created and respective Integer, Boolean, Byte, Double, Float, Gaussian, Long values are generated. Note that when you run the program, the same output might not come. For every run the output will be different.

THINGS TO TRY
  • In the nextInt method, replace 3 in place of 5 to get the output of integer range in between 0 to 2.
  • After LINE A, call nextInt multiple times and see that different integers are returned for multiple calls.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App