Java program to generate random numbers

Java Programming Java8Java Technologies Object Oriented Programming

The java.util.Random class instance is used to generate a stream of pseudorandom numbers.

  • The class uses a 48-bit seed, which is modified using a linear congruential formula.
  • The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudo randomly generated bits.
  • The nextInt() method of this class is used to get the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

Example

import java.util.*;
public class RandomNumberGeneration {
   public static void main( String args[] ) {
      // create random object
      Random randomno = new Random();
      Scanner sc = new Scanner(System.in);
      System.out.println("How many random numbers do you need :");
      int n= sc.nextInt();
     
      // check next int value
      for(int i=0; i<n; i++){
         System.out.println(randomno.nextInt());
      }
   }
}

Output

How many random numbers do you need :
4
709914869
-1882917719
-758130689
637944833


raja
Published on 25-Apr-2018 17:10:55
Advertisements