Java program to generate random numbers


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


Updated on: 13-Mar-2020

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements