0

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.");
        }
Gigabyted
  • 33
  • 7
  • Looking at the logic, if you're seeing fewer than 100,000 entries in your file, then `j >= 0` is not always true. Did you test for that? (e.g. with a `throw` or a `System.err.println` or something) – Mike 'Pomax' Kamermans May 04 '20 at 19:06
  • What makes you sure it generates only that few? I am no java developer, but don`t you need to flush and correctly close the streams at the end? Can't it happen that the process is terminated before the buffer is written to the disk? – ZorgoZ May 04 '20 at 19:08
  • Nothing can be said for sure without the full code. – Arvind Kumar Avinash May 04 '20 at 19:11
  • Oh! Yes yes yes it is because I didn't close the buffer. Thank you sir – Gigabyted May 04 '20 at 19:11

1 Answers1

0

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

ZorgoZ
  • 2,974
  • 1
  • 12
  • 34
  • You should use [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html), so there's no possibility to forget to close the stream, or to mishandle any exceptions which might be thrown. – Andy Turner May 04 '20 at 20:22
  • @AndyTurner great suggestion. But you should post it to the original question not to my answer. Or even as an answer. – ZorgoZ May 05 '20 at 06:51
  • but it's a suggestion for your answer: "you should properly close the stream": how do you do it *properly*? – Andy Turner May 05 '20 at 07:22
  • @AndyTurner then it is an improvement of my answer, but you wrote that _I should use_. I am not using anything. I am just pointing OP to the problem's origin. OP should follow your suggestion, not me :) – ZorgoZ May 05 '20 at 07:32