// Generates a random prime with a large number of digits (see DIGITS constant) import java.util.*; import java.math.*; public class Prime { public static final int DIGITS = 200; // number of digits for prime public static void main(String[] args) { Random r = new Random(); // start the string as a non-0 char String s = "" + (1 + r.nextInt(9)); // then add DIGITS-2 digits for (int i = 0; i < DIGITS - 2; i++) s += r.nextInt(10); // and a final 1 to make sure it's odd BigInteger n = new BigInteger(s + "1"); // then add 2 to it while it's not prime BigInteger two = new BigInteger("2"); while (!n.isProbablePrime(30)) n = n.add(two); // and print it System.out.println(n); } }