I am attempting to create a text file that contains 1 billion randomly-ordered non-repeating numbers. I have created the following code but I run out of memory long before completing (heap full). I am looking for suggestions or code corrections on how I can go about creating this txt file.
private int maxSize = 1000000000;
private int minimum = 1;
try {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for(int i = minimum - 1; i < maxSize; i++){
arrayList.add( i);
}
numlist.close();
// shuffle 10 times for true mix up
for(int j = 0; j < 10; j++){
Collections.shuffle(arrayList);
}
BufferedWriter numlist = new BufferedWriter(new FileWriter("randomNumbersNoRepeats.txt"));
for(int i = minimum - 1; i < maxSize; i++){
System.out.println(i);
numlist.write(i + ",");
}
numlist.close();
} catch (Exception e) {
System.out.println("Error in creating writer new bufferWriter"
+ " for randomNumbersNoRepeats.txt");
}