Java Algorithms How to - Generate very large random numbers








Question

We would like to know how to generate very large random numbers.

Answer

import java.math.BigInteger;
import java.util.Random;
//  www  . ja va 2s.  c  o m
public class Main {
  public static void main(String... a) {
    int n = 16;

    Random r = new Random();
    byte[] b = new byte[n];
    r.nextBytes(b);
    BigInteger i = new BigInteger(b);

    System.out.println(i);
  }
}

The code above generates the following result.