英文:
Why won't my random string generator generate 100,000 strings consistently?
问题
"我为课程制作了一个随机字符串生成器,因为我需要一个非常大的数据集来运行排序的效率测试。由于某种原因,它有时只会生成大约93,000到99,000个字符串,即使设置循环运行100,000次。我唯一能想到的是某种内存问题,但我不知道如何修复。
“'生成一个长度介于4到8之间的随机字符串,然后用随机小写字符生成该长度的字符串。这在此运行NO_OF_STR次,即100,000次。”
public static void main(String args[]) throws IOException {
final int NO_OF_STR = 100000;
final int STR_SIZE_MIN = 3;
final int STR_SIZE_MAX = 8;
char[] alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
BufferedWriter writer = new BufferedWriter(new FileWriter("/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt"));
BufferedReader reader = new BufferedReader(new FileReader("/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt"));
int bound = 0;
Random ran1 = new Random();
Random ran2 = new Random();
int test = 0;
String dupliTest;
String[] dupliTestArr = new String[NO_OF_STR];
for (int i = 0; i < NO_OF_STR; i++) {
bound = ran1.nextInt(STR_SIZE_MAX - STR_SIZE_MIN) + STR_SIZE_MIN;
for (int j = bound; j >= 0; j--) {
writer.write(alpha[ran2.nextInt(26)]);
}
writer.write("\n");
test++;
//System.out.println("String #" + (i + 1) + " generated.");
}
}
英文:
I made a random string generator for a class, as I need a very large dataset to run efficiency tests for sorts. For some reason, it sometimes will only generate ~93,000 - 99,000 strings, even though it is set up to run the loop 100,000 times. The only thing I can think of is some sort of memory issue, but I don't know how to fix it.
"Generate a random string length between 4 and 8, then generate a string of that length with random lowercase chars. It runs NO_OF_STR times, which is 100,000 here."
final int NO_OF_STR = 100000;
final int STR_SIZE_MIN = 3;
final int STR_SIZE_MAX = 8;
char[] alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
BufferedWriter writer = new BufferedWriter(new FileWriter("/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt"));
BufferedReader reader = new BufferedReader(new FileReader("/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt"));
int bound = 0;
Random ran1 = new Random();
Random ran2 = new Random();
int test = 0;
String dupliTest;
String[] dupliTestArr = new String[NO_OF_STR];
for (int i = 0; i < NO_OF_STR; i++) {
bound = ran1.nextInt(STR_SIZE_MAX - STR_SIZE_MIN) + STR_SIZE_MIN;
for (int j = bound ; j >= 0; j--) {
writer.write(alpha[ran2.nextInt(26)]);
}
writer.write("\n");
test++;
//System.out.println("String #" + (i + 1) + " generated.");
}
答案1
得分: 0
你应该正确关闭流。如果不这样做,在进程终止之前,数据可能不会被刷新到磁盘上。
英文:
You should properly close the stream. If not, might not be flushed to disk before the process is terminated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论