D std.random different behavior between integer and decimal uniform random number generation

ccjuju

I'm generating some seeded random numbers in D using the default random number generation engine, and I'm getting some "slightly off" values depending on whether I request integer or floating point/double random number generation. For example this code:

import std.random; // From docs: alias Random = MersenneTwisterEngine!(uint, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18).MersenneTwisterEngine;
int seed = 123;
Random genint = Random(seed);
Random gendouble = Random(seed);
foreach (i; 0..4) {
    writefln("rand[%d]: %d %f", i, uniform(0, 1000000, genint), uniform(0.0, 1000000.0, gendouble));
}

generates these results:

rand[0]: 696626 696469.187433
rand[1]: 713115 712955.321584
rand[2]: 286203 286139.338810
rand[3]: 428567 428470.925062

For some reason they're close but off by a noticeable amount. Is this expected behavior? I'm not completely familiar with the method being used to generate the numbers so I'm wondering what might be causing this to occur. Specifically, the reason I'm curious about this is I'm trying to port some D code to Objective-C, using the MTRandom library:

int seed = 123;
MTRandom *genint = [[MTRandom alloc] initWithSeed:seed];
MTRandom *gendouble = [[MTRandom alloc] initWithSeed:seed];
for (int i = 0; i < 4; i++) {
    NSLog(@"rand[%d]: %d %f", i, [genint randomUInt32From:0 to:1000000], [gendouble randomDoubleFrom:0.0 to:1000000.0]);
}

and I'm getting these results:

rand[0]: 696469 696469.187433
rand[1]: 712956 712955.321584
rand[2]: 286139 286139.338810
rand[3]: 428471 428470.925062

Identical for retrieving random doubles, but here the integer results are much closer.. though sometimes off by one! Is 712955.32 supposed to be rounded up to 712956 using this method of random generation? I just don't know...

What's the best way I can ensure that my seeded random integer generation is identical between D and Objective-C?

BCS

The following is based on a very quick review of the docs page:

I don't see where it's indicated that the results of uniform!int and uniform!double should even be remotely close, nor any specification on how the bits from the random number generator get converted to the requested type.

In short, I think the ints and doubles being close is an implementation detail and you can not depend on it. Furthermore, I'd not depend on the obj-c and D implementations returning the same sequence of doubles, even from the same seed. (I expect you can count of the MT construct producing the same bit stream however.)

If you need the same sequence, then you will need to crack open the obj-c version and port it to D as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

D std.random different behavior between integer and decimal uniform random number generation

From Dev

Understanding uniform random number generation

From Dev

C++ thread-safe uniform distribution random number generation

From Dev

Spark - Random Number Generation

From Dev

C - random number generation

From Dev

Random number Generation with Seed

From Dev

Random number generation with in a range

From Dev

generation of random number in MATLAB

From Dev

Random number generation algorithm

From Dev

generation of random number in MATLAB

From Dev

Random number generation in Solaris?

From Dev

pseudo random number generation

From Dev

Random Number generation with GameplayKit

From Dev

Random Number Generation in R

From Dev

Random number generation with seeds

From Dev

Unexpected behavior with random array generation

From Java

How does one make random number between range for arc4random_uniform()?

From Dev

how to make a random generation number between 1 to 9 without repitition

From Dev

Generating a random integer with non-uniform distribution

From Dev

Grokking Random Number Generation in Haskell

From Dev

Random number generation on card and on terminal?

From Dev

Random Number Generation in SQL Server

From Dev

Using engines for random number generation

From Dev

Random number generation using srand()

From Dev

Role of seed in random number generation

From Dev

User trustable random number generation

From Dev

Constraining random number generation in Python

From Dev

Random number generation function not work

From Dev

Generate random number between 1 and 3 / including 1 decimal place

Related Related

HotTag

Archive