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

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

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次。”

  1. public static void main(String args[]) throws IOException {
  2. final int NO_OF_STR = 100000;
  3. final int STR_SIZE_MIN = 3;
  4. final int STR_SIZE_MAX = 8;
  5. 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'};
  6. BufferedWriter writer = new BufferedWriter(new FileWriter("/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt"));
  7. BufferedReader reader = new BufferedReader(new FileReader("/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt"));
  8. int bound = 0;
  9. Random ran1 = new Random();
  10. Random ran2 = new Random();
  11. int test = 0;
  12. String dupliTest;
  13. String[] dupliTestArr = new String[NO_OF_STR];
  14. for (int i = 0; i < NO_OF_STR; i++) {
  15. bound = ran1.nextInt(STR_SIZE_MAX - STR_SIZE_MIN) + STR_SIZE_MIN;
  16. for (int j = bound; j >= 0; j--) {
  17. writer.write(alpha[ran2.nextInt(26)]);
  18. }
  19. writer.write("\n");
  20. test++;
  21. //System.out.println("String #" + (i + 1) + " generated.");
  22. }
  23. }
英文:

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."

  1. final int NO_OF_STR = 100000;
  2. final int STR_SIZE_MIN = 3;
  3. final int STR_SIZE_MAX = 8;
  4. 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;};
  5. BufferedWriter writer = new BufferedWriter(new FileWriter(&quot;/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt&quot;));
  6. BufferedReader reader = new BufferedReader(new FileReader(&quot;/Users/gigabyted/Documents/Projects/Eclipse/RandomStringGenerator/src/strings.txt&quot;));
  7. int bound = 0;
  8. Random ran1 = new Random();
  9. Random ran2 = new Random();
  10. int test = 0;
  11. String dupliTest;
  12. String[] dupliTestArr = new String[NO_OF_STR];
  13. for (int i = 0; i &lt; NO_OF_STR; i++) {
  14. bound = ran1.nextInt(STR_SIZE_MAX - STR_SIZE_MIN) + STR_SIZE_MIN;
  15. for (int j = bound ; j &gt;= 0; j--) {
  16. writer.write(alpha[ran2.nextInt(26)]);
  17. }
  18. writer.write(&quot;\n&quot;);
  19. test++;
  20. //System.out.println(&quot;String #&quot; + (i + 1) + &quot; generated.&quot;);
  21. }

答案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:

确定