[Issue 15147] New: std.random.uniform return value depends on integer size

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Oct 3 11:25:09 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=15147

          Issue ID: 15147
           Summary: std.random.uniform return value depends on integer
                    size
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: thecybershadow at gmail.com

With an RNG seeded with a constant value, uniform(0, 100) and uniform(0, 100L)
will return different values.

Demonstration:

////////////////// test.d //////////////////
import std.random;
import std.stdio;

void test(T)()
{
    Mt19937 rng;
    foreach (n; 0..10)
        write(uniform(0, T(100), rng), " ");
    writeln();
}

void main()
{
    test!int();
    test!uint();
    test!long();
    test!ulong();
}
////////////////////////////////////////////

This will print:

12 2 34 85 4 91 29 85 98 3
12 2 34 85 4 91 29 85 98 3
54 49 75 69 11 25 66 64 28 18
54 49 75 69 11 25 66 64 28 18

This can be a problem for applications that expect seeded RNGs to behave
predictably on all platforms (e.g. to generate something procedurally than must
be in sync), and pass a size_t to uniform. E.g.:

string sample = arr[uniform(0, $, rng)];

--


More information about the Digitalmars-d-bugs mailing list