You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are random number generators that generate floating point numbers directly (e.g. dSFMT). Currently, implementing the rand::Rng trait requires next_u32 and possibly next_u64 (which must be implemented to be uniformly distributed) so these random number generators require some very inefficient contortions to fit into this interface, and don't get any speed benefit when generating f32s or f64s since everything goes via next_u32 and next_u64.
I propose changing rand to:
implRandforf32{fnrand<R:Rng>(rng:&mutR) -> f32{
rng.next_f32()}}traitRng{// .../// Override if your RNG can generate [0, 1) numbers more efficiently.fnnext_f32(&mutself) -> f32{(rng.next_u32() >> 8)asf32 / (1 << 24)asf32}}
and similarly for f64. This means a RNG that creates floating point numbers directly can override next_f32 and next_f64 so that generic users reap the benefits.
Random numbers are often used for numeric simulations, and so this seems like a sensible addition, and I cannot think of any other core types that deserve an explicit specialisation in this form so this shouldn't be a slippery slope.
The text was updated successfully, but these errors were encountered:
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).
Fixesrust-lang/rfcs#425.
There are random number generators that generate floating point numbers directly (e.g. dSFMT). Currently, implementing the
rand::Rng
trait requiresnext_u32
and possiblynext_u64
(which must be implemented to be uniformly distributed) so these random number generators require some very inefficient contortions to fit into this interface, and don't get any speed benefit when generatingf32
s orf64
s since everything goes vianext_u32
andnext_u64
.I propose changing
rand
to:and similarly for
f64
. This means a RNG that creates floating point numbers directly can overridenext_f32
andnext_f64
so that generic users reap the benefits.Random numbers are often used for numeric simulations, and so this seems like a sensible addition, and I cannot think of any other core types that deserve an explicit specialisation in this form so this shouldn't be a slippery slope.
The text was updated successfully, but these errors were encountered: