1. 20
  1.  

  2. 3

    This was pretty funny right up to the part where it gave something very similar to the seeding method I use :/

    FWIW I use:

    srand((unsigned int)getpid() ^ (unsigned int)tv.tv_sec ^ (unsigned int)tv.tv_usec));

    Does anyone know of a better way of doing this that’s also POSIX compliant? I mostly just want to make sure that my program gets different random numbers each time.

    1. 2

      I’d include my own copy of something like MT. It’s not much code. (Well, I personally just always use arc4random, even for toy programs like counting how many points are in a unit circle to experimentally determine pi). Seed it with time or pid if you like.

      rand and random have infamously been so bad that you probably don’t want to use them. For any purpose. For example, some implementations have a habit of producing alternating large and small numbers, which would result in very few points near the corners of a square, and therefore the wrong value of pi.

      I think “I have brought my own deterministic ‘random’ number generator” provides a lot of clarity about author’s purpose and understanding.

      I mostly just want to make sure that my program gets different random numbers each time.

      That’s what a lot of people want. Hence the change to an arc4random backend.

      1. 3

        In the face of arc4random being BSD only, is the correct thing on Linux environments to -lbsd?

        1. 2

          https://github.com/nmathewson/libottery-lite is sort of converging into arc4random as well. The libbsd version is growing old.