Search code examples
javarandomintlong-integerfactorization

Java: Produce larger random numbers


I created a simple prime factorization program in java for the fun of it. Right now I am using the Random class and nextLong() method to assign "temp" a random number ranging in the quintillions and factors it amazingly quickly. What data type or algorithm or method should I use to obtain significantly larger values?

import java.util.Random;
import javax.swing.JOptionPane;

public class factor {
public static void main(String[] args) {
   Random gen = new Random();
   String factors = "";
   long temp = 0;
   String hello = JOptionPane.showInputDialog("Type a random number(Must be smaller than 4,611,686,018,427,387,904), or type 1 for a random large number.");
  temp = Long.parseLong(hello);
   if(temp < 2)
   temp = Math.abs(gen.nextLong());
   long temp2 = temp;
   System.out.println("  The factors of   \n\n  " + temp2 + "   are:");
   System.out.println("");
   while(temp != 1){
       //System.out.print(temp);
   for(long ii = 2; ii <= (Math.ceil(Math.sqrt(temp))); ii++){
  // if(ii%12345 == 0)
 // System.out.println(temp + " " + ii + " " + factors);
        if(temp%ii == 0){
            factors = factors + "  " + ii;
           // System.out.println(temp + " " + ii + " " + factors);
            temp = temp/ii;
            ii = temp + 1;       
  }else{
   if(ii == Math.ceil(Math.sqrt(temp))){
   factors = factors + " " + temp;
            System.out.println(factors);
            temp = 1;
            ii = temp + 1;   
   }}
        }
   }}}

Solution

  • You can use the random constructor for BigInteger:

    BigInteger(int numBits, Random rnd)
    

    If you need any more information, you can check out the API: BigInteger