3

So I was creating a program that would call a function and return 0 or 1 (0 meaning tails and 1 meaning heads) and then use that to print the outcome of 100 flips.

It seemed simple enough thinking I could use srand(time(NULL)) to seed rand() with constantly varying seeds. Here was my first crack.

#include <stdio.h>
#include <stdlib.h>
int flip();


int main(void) {

    int heads = 0;
    int tails = 0;

    for (short int count = 1; count <= 100; ++count) {

        int number = flip();

        if (number == 0) {
            printf("%s", "Tails");
            ++tails;
        }
        else if (number == 1) {
            printf_s("%s", "Heads");
            ++heads;
        }

    }//end for
    printf_s("\n%d Tails\n", tails);
    printf_s("%d Heads", heads);
}//end main

int flip(void) {

    srand(time(NULL));
    int number = (int)rand();
    printf("%d", number%2);
    return number%2;
}//end flip

I would run the program and my rand() value would always be a five digit integer repeated in each iteration of the for statement (i.e 15367, 15745, or 15943).

I messed around until I discovered changing srand(time(NULL)) to srand(time(NULL)*time(NULL)/rand()) did the trick.

My only thought is that the time between each for iteration is so small the the time(NULL) part of the srand() function doesn't change enough to feed a different seed value.

I also tried srand(time(NULL)/rand()), however, this produced the same result (52 heads 48 tails) every time I ran the program (20+times); however, the rand() values were all different from each other.

I do not know why these things happened, or why the final srand(time(NULL)*time(NULL)/rand()) function worked, and I would love it if someone could explain!

Ton
  • 49
  • 1
  • 3
  • check this one: [How can I generate different random numbers for each player?](https://stackoverflow.com/q/36558716/2173917) – Sourav Ghosh Oct 18 '18 at 07:48
  • 2
    Normally, you'd call srand only *once* and afterwards call rand solely. – Aconcagua Oct 18 '18 at 07:49
  • 1
    Srand seeds the PRNG. It is meant to be called once at application start. – rustyx Oct 18 '18 at 07:49
  • or this one [rand() is consistent across multiple function calls](https://stackoverflow.com/a/29478570/2173917) – Sourav Ghosh Oct 18 '18 at 07:49
  • `rand` being used with % does not give you good distribution. You'd be better off with `return number > RAND_MAX/2;` (generally, number*desiredMax/RAND_MAX is a good approach - provided you handle possibly occurring overflow correctly). – Aconcagua Oct 18 '18 at 07:51

1 Answers1

2

The reason is, that time(NULL) changes only once per second! This means, that you seed the random number generator 100 times with the same seed. A better way is to seed the RNG only once at start of the process (at the head of main(), then you should get different values.

If you start your program more often than once a second, you could also seed it with

srand(time(NULL)+getpid());

or similar.

Ctx
  • 18,090
  • 24
  • 36
  • 51