RandomPrime.java


Below is the syntax highlighted version of RandomPrime.java from §5.6 Cryptography.


/******************************************************************************
 *  Compilation:  javac RandomPrime.java
 *  Execution:    java RandomPrime N
 *  
 *  Generate a N-bit integer that is (probably) prime.
 *
 ******************************************************************************/

import java.math.BigInteger;
import java.util.Random;
import java.security.SecureRandom;
    
public class RandomPrime {
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        SecureRandom random = new SecureRandom();
        BigInteger prime = BigInteger.probablePrime(N, random);
        StdOut.println(prime);
    }
}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.