EDIT
The random number generator in this code is a blackbox for a similar algorithm that isn't publishable for privacy concern. The algorithm works in similar way, in that it takes in a seed value and generates number.
Simplifying the question
The corrupted references are from a text file, there are thousands of those reference numbers. I'm tasked to create a program that will take in one reference from the text file, then brute force the algorithm through to find the seed that generated the number.
The generated numbers are in reversed, so just brute forcing will not get you the right number, you will have to reverse the brute number and check if it matches with the reference number, if yes then print the seed number and get the next reference number.
Since the seeds value are from 100 million to 999 million, we have to brute force through those to find the right seed value. it takes 4 minute to brute force a single reference number and I have hundreds of thousands of the numbers to go through. How can I make it super fast?
I tried putting the references in arraylist on first load since it's just 900 million list of reference and then use indexOF, but it gives me outofmemory error.
Product Version: Apache NetBeans IDE 9.0 (Build incubator-netbeans-release-334-on-20180708)
Java: 11.0.1; Java HotSpot(TM) 64-Bit Server VM 11.0.1+13-LTS
Runtime: Java(TM) SE Runtime Environment 11.0.1+13-LTS
System: Mac OS X version 10.13.6 running on x86_64; UTF-8; en_GB (nb)
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.ArrayList;
/**
*
* @author HJoe
*/
public class idResolver {
/**
* @param args
* @throws java.io.FileNotFoundException
* @throws java.io.UnsupportedEncodingException
*/
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
ArrayList corruptedReferences = new ArrayList();
Stream<String> lines = Files.lines(Paths.get("/Users/kingamada/Documents/Corrupted References.txt"));
lines.forEach(l -> {
corruptedReferences.add(l);
});
StringBuilder sb = new StringBuilder();
int max = 1000000000;
int min = 100000000;
long nano_startTime = System.nanoTime();
long millis_startTime = System.currentTimeMillis();
for (int i = 0; i < corruptedReferences.size(); i++) {
long corruptIDNumbers =corruptedReferences.get(i);
for (int n = min; n < max; n++) {
Random rand = new Random(n);
long findID = rand.nextLong();
sb.append(findID);
//String reverseID = reverse(String.valueOf(findID));
findID = Long.valueOf(sb.reverse().toString().substring(0, 9));
if (findID == corruptIDNumbers) {
//System.out.println("Found the ID: "+n);
break;
}
sb.setLength(0);
}
long nano_endTime = System.nanoTime();
long millis_endTime = System.currentTimeMillis();
System.out.println("Time taken in nano seconds: "
+ (nano_endTime - nano_startTime));
System.out.println("Time taken in milli seconds: "
+ (millis_endTime - millis_startTime));
}
}
public static String reverse(String input) {
char[] in = input.toCharArray();
int begin = 0;
int end = in.length - 1;
char temp;
while (end > begin) {
temp = in[begin];
in[begin] = in[end];
in[end] = temp;
end--;
begin++;
}
return new String(in);
}
public static long reverseID(Long idNumber) {
String s = String.valueOf(idNumber);
StringBuilder sb = new StringBuilder();
for (int n = 0; n < s.length(); n++) {
sb.append(s.charAt((s.length() - n) - 1));
}
s = sb.toString();
long reversedNumber = Long.valueOf(s);
return reversedNumber;
}
}