Sunday, 29 October 2017

java.util.Random class in java

Random Class



The random class instance is used to generate a stream of the pseudorandom number.

The class uses a 48-Bit seed, which is modified using a liner confidential formula.

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

It is also used to generate the number randomly.

It uses the algorithm.

Constructors


1). Random()

This creates a new random number generator.

2). Random(long value) 

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


Methods


1).protected intnext(int bits)


The protected in next method generates the next pseudorandom number.
    

2). boolean nextBoolean() 

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

3). void nextBytes(byte[] bytes) 

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

4). 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.
    

5). 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.
    

6). 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.
    

7). int nextInt() 

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

8). int nextInt(int n) 

This method returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
    
9). long nextLong() 

This method returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
    
10). void setSeed(long seed) 

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


Example


import java.util.*;

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

     {
          Random r1=new Random();

          System.out.println(r.nextInt(10));
      
      }
}
         

         














Popular posts