Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3451962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:12:08+00:00 2026-05-18T09:12:08+00:00

All, I have implemented a code that generates 2 random prime numbers and the

  • 0

All,

I have implemented a code that generates 2 random prime numbers and the multiplication of these 2 numbers should pass the Miller Rabin primality test. However, my code keeps looping all the time trying to find a number that passes Miller rabin test and ends up with a Stackoverflow exception. Here is the code:

private void populateRandomPrimes()
{
    onePrimeValue = RandomPrime.getValue();

    do
    {
        secondPrimeValue= RandomPrime.getValue();
    }while(onePrimeValue == secondPrimeValue);

    BigInteger calcNum = new BigInteger(Integer.toString(onePrimeValue*secondPrimeValue));

    try
    {
        **if (calcNum.isProbablePrime(20))**
            populateMultiplicativeForPlayer();
        else
            populateRandomPrimes();
    }
    catch (Exception io)
    {
        io.printStackTrace();
    }
}

In the code above:
1 > RandomPrime class returns a random prime number
2 > Both onePrimeValue and secondPrimeValue should be different
3 > Since the code line : if (calcNum.isProbablePrime(20)) never returns a true, I end up calling the same till I get Stackoverflow exception

Can anyone suggest me how to get around with this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:12:09+00:00Added an answer on May 18, 2026 at 9:12 am

    Please see my comment below your question…

    private void populateRandomPrimes()
    {
        while (true){
          onePrimeValue = RandomPrime.getValue();
    
          do
          {
              secondPrimeValue= RandomPrime.getValue();
          }while(onePrimeValue == secondPrimeValue);
    
          BigInteger calcNum = new BigInteger(Integer.toString(onePrimeValue*secondPrimeValue));
    
          try
          {
              if (calcNum.isProbablePrime(20)){
                  populateMultiplicativeForPlayer();
                  return;
              }
          }
          catch (Exception io)
          {
              io.printStackTrace();
          }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented VirtualPathProvider class so I can keep all my views in the
If have a set of classes that all implement an interface. interface IMyinterface<T> {
I have a bunch of projects in parallel subdirectories that all have etc/lighttpd.conf files.
I have a MFC dialog with 32 CComboBoxes on it that all have the
So I have a form with lots of controls that all have a tab
I have developed some classes with similar behavior, they all implement the same interface.
I'm sure we all have received the wonderfully vague Object reference not set to
I have created a bunch of movie clips which all have similar names and
I have many emails coming in from different sources. they all have attachments, many
My Situation I have a N rectangles The rectangles all have the same shape

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.