Java.math.BigInteger.probablePrime() Method



Description

The java.math.BigInteger.probablePrime(int bitLength, Random rnd) returns a positive BigInteger that is probably prime, with the specified bitLength. The probability that a BigInteger returned by this method is composite does not exceed 2-100.

Declaration

Following is the declaration for java.math.BigInteger.probablePrime() method.

public static BigInteger probablePrime(int bitLength, Random rnd)

Parameters

  • bitLength − bitLength of the returned BigInteger.

  • rnd − Source of random bits used to select candidates to be tested for primality.

Return Value

This method returns a BigInteger of bitLength bits that is probably prime.

Exception

ArithmeticException − If bitLength < 2.

Example

The following example shows the usage of math.BigInteger.probablePrime() method.

package com.tutorialspoint;

import java.math.*;
import java.util.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create a BigInteger object
      BigInteger bi;

      // create and assign value to bitLength
      int bitLength = 3;

      // create a random object
      Random rnd = new Random();

      // assign probablePrime result to bi using bitLength and rnd
      // static method is called using class name
      bi = BigInteger.probablePrime(bitLength, rnd);

      String str = "ProbablePrime of bitlength " + bitLength + " is " +bi;

      // print bi value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result −

ProbablePrime of bitlength 3 is 5
java_math_biginteger.htm
Advertisements