Archiloque

Du code et des loutres

  • random in the wild
    http://www.tedunangst.com/flak/post/random-in-the-wild

    Before we get to the fun parts, though, a bit of serious commentary. The rand and random functions are specified by C and POSIX, respectively, to have initial seeds of 1 and to be reseedable via srand and srandom. That has been interpreted to mean that the same seed must always produce the same sequence, but the actual guarantee is quite a bit weaker than that. The C standard says very little about the rand algorithm, and POSIX doesn’t add much except random must be “a non-linear additive feedback random-number generator”. The key point, however, is that there are many such generators, all of which produce different sequences even with identical seeds. Different operating systems are not guaranteed to produce the same sequence. In fact, repeated executions of the same program are not guaranteed to produce the same sequence. Each execution could pick different internal multipliers. Or a fully conforming implementation may well contain seed ^= getpid() at the top of srandom. Repeatable within the program, but not afterwards. The same sequence language only applies within the execution of a single process. Any program which depends on generating the same sequence with the same seed after multiple executions is in fact depending on undocumented, nonstandard, implementation specific behavior. Here’s a nickel kid; get a better language lawyer.

    But who cares about standards? Let’s look at some code.