Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java.math.BigInteger.probablePrime() method in Java

  • Difficulty Level : Basic
  • Last Updated : 04 Dec, 2018

Prerequisite : BigInteger Basics

The probablePrime() method will return a Biginteger of bitLength bits which is prime. bitLength is provided as parameter to method probablePrime() and method will return a prime BigInteger of bitLength bits. The probability that a BigInteger returned by this method is composite and does not exceed 2^-100.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.


public static BigInteger probablePrime(int bitLength, Random rnd)

Parameters: This method accepts two parameters as shown in the above syntax and described below.

  • 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.


  • ArithmeticException – if bitLength < 2.

Below program illustrate the probablePrime() method:

import java.math.*;
import java.util.Random;
import java.util.Scanner;
public class GFG {
    public static void main(String[] args)
        Scanner sc = new Scanner(;
        // create a BigInteger object
        BigInteger biginteger;
        // create a integer value for bitLength
        int length = 4;
        // create a random object
        Random random = new Random();
        // call probablePrime method to find next probable prime
        // whose bit length is equal to bitLength provided as parameter.
        biginteger = BigInteger.probablePrime(length, random);
        String result = "ProbablePrime whose bit length is "
                        + length + " = " + biginteger;
        // print result value


ProbablePrime whose bit length is 4 = 13

Reference:, %20java.util.Random)

My Personal Notes arrow_drop_up
Recommended Articles
Page :