Skip to content

rand: Add next_f64/f32 to Rng. #18534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2014
Merged

rand: Add next_f64/f32 to Rng. #18534

merged 1 commit into from
Nov 4, 2014

Conversation

huonw
Copy link
Member

@huonw huonw commented Nov 2, 2014

Some random number generates output floating point numbers directly, so
by providing these methods all the functionality in librand is available
with high-performance for these things.

An example of such an is dSFMT (Double precision SIMD-oriented Fast
Mersenne Twister)
.

The choice to use the open interval [0, 1) has backing elsewhere,
e.g. GSL (GNU Scientific Library) uses this range, and dSFMT supports
generating this natively (I believe the most natural range for that
library is [1, 2), but that is not totally sensible from a user
perspective, and would trip people up).

Fixes rust-lang/rfcs#425.

Some random number generates output floating point numbers directly, so
by providing these methods all the functionality in librand is available
with high-performance for these things.

An example of such an is dSFMT (Double precision SIMD-oriented Fast
Mersenne Twister).

The choice to use the open interval [0, 1) has backing elsewhere,
e.g. GSL (GNU Scientific Library) uses this range, and dSFMT supports
generating this natively (I believe the most natural range for that
library is [1, 2), but that is not totally sensible from a user
perspective, and would trip people up).

Fixes rust-lang/rfcs#425.
@@ -124,23 +120,22 @@ macro_rules! float_impls {
// the precision of f64/f32 at 1.0), so that small
// numbers are larger than 0, but large numbers
// aren't pushed to/above 1.
Open01(((rng.$method_name() >> $ignored_bits) as $ty + 0.25) / SCALE)
Open01(rng.$method_name() + 0.25 / SCALE)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not knowledgeable about the topic but does this in any way skew random streams that are otherwise evenly distributed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does; there is precisely one value that doubles in frequency: 0.25. Let eps be the floating point precision, e.g. 2-53 for f64 and x = eps/4 be the value added here, then 0.25 = 0.25 + x = 0.25' + x where 0.25' = 0.25 - x is the largest float less than 0.25.

However, this is doubling from 1 in 2-53 to 1 in 2-52 (or 24 → 23 for f32) which is rather small. It's exceedingly unlikely that a consumer of f64's will see 0.25 even once (but rather less unlikely for f32's).

I think I investigated this before for the original implementation to see what was done elsewhere, but I have forgot it, and did not record my findings.

(Note that this line isn't actually changing the functionality.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dSFMT operates on numbers between 1.0 and 2.0, just ors with 1 in the smallest bit and then subtracts 1.0 to lie in the open interval (0.0, 1.0) (e.g. 0 becomes 2-53). That behaviour halves the output space, but it does not lead to a bias.

GSL does rejection sampling (calling the RNG until 0 is not encountered, which is rather likely). I guess we should consider doing that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @huonw!

@alexcrichton
Copy link
Member

From an API-point-of-view it is somewhat unfortunate to see gen<T: Rand> as well as things like next_u32 and next_u64 (plus the float versions now). It would be kinda cool if Rng implementors could override gen<u32> or gen<u64>, but that's very pie in the sky :)

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Nov 4, 2014
@bors bors merged commit e49be7a into rust-lang:master Nov 4, 2014
@huonw huonw deleted the next-floats branch November 25, 2014 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider adding next_f32/next_f64 methods to rand::Rng
3 participants