get Random BigInteger - Java java.math

Java examples for java.math:BigInteger

Description

get Random BigInteger

Demo Code


import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arraycopy;
import java.util.Random;
import static java.math.BigInteger.ONE;
import static java.math.BigInteger.ZERO;

public class Main{
    public static void main(String[] argv) throws Exception{
        BigInteger limit = new BigInteger("1234");
        System.out.println(getRandom(limit));
    }/*from w  w w .ja  va2s  .c  o  m*/
    public static BigInteger getRandom(BigInteger limit) {
        return getRandom(limit, new SecureRandom());
    }
    public static BigInteger getRandom(BigInteger limit, Random random) {
        BigInteger result;
        do {
            result = new BigInteger(limit.bitLength(), random);
        } while (limit.compareTo(result) >= 0);
        return result;
    }
}

Related Tutorials