为什么我的随机字符串生成器不能稳定生成10万个字符串?

huangapple go评论175阅读模式
英文:

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 = {&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;,&#39;h&#39;,&#39;i&#39;,&#39;j&#39;,&#39;k&#39;,&#39;l&#39;,&#39;m&#39;,&#39;n&#39;,&#39;o&#39;,&#39;p&#39;,&#39;q&#39;,&#39;r&#39;,&#39;s&#39;,&#39;t&#39;,&#39;u&#39;,&#39;v&#39;,&#39;w&#39;,&#39;x&#39;,&#39;y&#39;,&#39;z&#39;};
		BufferedWriter writer = new BufferedWriter(new FileWriter(&quot;/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt&quot;));
		BufferedReader reader = new BufferedReader(new FileReader(&quot;/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt&quot;));
		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 &lt; NO_OF_STR; i++) {
			bound = ran1.nextInt(STR_SIZE_MAX - STR_SIZE_MIN) + STR_SIZE_MIN;
			for (int j = bound ; j &gt;= 0; j--) {
				writer.write(alpha[ran2.nextInt(26)]);
			}
			writer.write(&quot;\n&quot;);
			
			test++;
			
			//System.out.println(&quot;String #&quot; + (i + 1) + &quot; generated.&quot;);
		}

答案1

得分: 0

你应该正确关闭流。如果不这样做,在进程终止之前,数据可能不会被刷新到磁盘上。

英文:

You should properly close the stream. If not, might not be flushed to disk before the process is terminated.

huangapple
  • 本文由 发表于 2020年5月5日 03:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61599663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定