1

Well, I wrote a little Program that should generate random values, but no value should be in the output file twice. On Linux it's running perfectly, but on Windows it just runs infinity long on the 32768th value. That means, that cmd is open but nothing really happens from that point.

I already did debug it 30 times by now but it never had any problem (it was hell do debug it) I wrote it new, recompiled it, even changed values under it was running through the debugger

Here is the Code:

 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 #include <stdbool.h>

int intlen(int[]);

int main(int argc, char *argv[]) {
    FILE *fp;
    int percent = 0;
    int Ergebnis = 0, length = 0, lenNow = 0;
    bool proof = true;
    srand(time(NULL));
    fp = fopen("ranGen.txt", "w");
    length = atoi(argv[1]);
    int Lookup[length];
    for (int x = 0; x < length; x++){
        Lookup[x] = 0;
    }
    for (int i = 0; i < length; i++) {
        do {
            proof = true;
            Ergebnis = rand() % (2147483646 - 1 + 1) + 1;

            for (int j = 0; j < length && Lookup[j] != 0 && proof != false; j++) {
                if (Ergebnis == Lookup[j]) {
                    proof = false;
                }
            }
        }while(proof == false);
        Lookup[lenNow] = Ergebnis;
        lenNow++;
        fprintf(fp,"%i ",Ergebnis);
    }
    return 0;
}

posted everything, but the output because I don't really know where the problem is and I think you will need the most of it reproduce my problem. if you compiled it, run it through cmd with something like 50000, so that it is higher than 32768.

(like this: example.exe 50000)

Expected was, that it will create a File named RanGen.txt with 200000 random values (200000 was my test value)

But the output was 32767 Values in the text Document and then the program just did nothing more.

Solution: used rand() % 214748346; instead of rand() % (214748346 - 1 + 1) + 1;

Diamond
  • 13
  • 3
  • What range do you expect `Ergebnis` to be? You should check what your `RAND_MAX` is. – Eugene Sh. Jul 09 '19 at 20:55
  • 7
    `RAND_MAX` in the C library you're using on Windows is 32,767, so `rand() % (2147483646 - 1 + 1) + 1` will generate numbers between 1 and 32,768 inclusive, and your `do` loop will therefore repeat forever, because you're telling it to. – Crowman Jul 09 '19 at 20:55
  • What would be a replacement for `rand()`? Would be great if I could use it on Linux and Windows – Diamond Jul 09 '19 at 20:59
  • 1
    `RAND_MAX` *is* on both Linux and Windows. It is standard. It might have different values though. – Eugene Sh. Jul 09 '19 at 21:00
  • @Diamond: You'll need to use a different random number generator. [This question](https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c/822361) can get you started. – Crowman Jul 09 '19 at 21:01
  • @PaulGriffiths Thank you very much, now it did work properly (^.^) – Diamond Jul 09 '19 at 21:09

1 Answers1

0

Looks like rand() is only 16 bits in that library. Make it 32 bits by calling it twice:

int rand32() {
    return rand() ^ (rand() << 16);
}

Also, consider eliminating the inner duplicate-search loop by using Bob Floyd's algorithm: https://blog.acolyer.org/2018/01/30/a-sample-of-brilliance/

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • Oh nice, I will consider it, thank you very much (^.^) – Diamond Jul 10 '19 at 00:04
  • Note that the range 0..32767 uses 15-bit numbers, not 16-bit numbers. It's the positive part of the range of 16-bit signed values. The code shown really only generates 30-bit numbers, and bits 15 and 31 will both be zero, always. A 32-bit random number generator that only generates positive numbers would have bit 15 set and not set randomly, of course. – Jonathan Leffler Jul 15 '19 at 13:22