@@ -1085,20 +1085,58 @@ impl SeedableRng for StdRng {
1085
1085
}
1086
1086
}
1087
1087
1088
- /// Create a weak random number generator with a default algorithm and seed.
1088
+ /// An RNG recommended when small state, cheap initialization and good
1089
+ /// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
1090
+ /// efficient on the current platform, without consideration for cryptography or
1091
+ /// security. The size of its state is much smaller than for `StdRng`.
1089
1092
///
1090
- /// It returns the fastest `Rng` algorithm currently available in Rust without
1091
- /// consideration for cryptography or security. If you require a specifically
1092
- /// seeded `Rng` for consistency over time you should pick one algorithm and
1093
- /// create the `Rng` yourself.
1093
+ /// Reproducibility of output from this generator is however not required, thus
1094
+ /// future library versions may use a different internal generator with
1095
+ /// different output. Further, this generator may not be portable and can
1096
+ /// produce different output depending on the architecture. If you require
1097
+ /// reproducible output, use a named RNG, for example `XorShiftRng`.
1094
1098
///
1095
- /// This will seed the generator with randomness from thread_rng.
1096
- #[ cfg( feature="std" ) ]
1097
- pub fn weak_rng ( ) -> XorShiftRng {
1098
- XorShiftRng :: from_rng ( & mut thread_rng ( ) ) . unwrap_or_else ( |err|
1099
- panic ! ( "weak_rng failed: {:?}" , err) )
1099
+ /// The current algorithm used on all platforms is [Xorshift].
1100
+ ///
1101
+ /// ```
1102
+ /// use rand::{SeedableRng, SmallRng, thread_rng};
1103
+ ///
1104
+ /// let _rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
1105
+ /// ```
1106
+ ///
1107
+ /// [Xorshift]: struct.XorShiftRng.html
1108
+ #[ derive( Clone , Debug ) ]
1109
+ pub struct SmallRng ( XorShiftRng ) ;
1110
+
1111
+ impl RngCore for SmallRng {
1112
+ fn next_u32 ( & mut self ) -> u32 {
1113
+ self . 0 . next_u32 ( )
1114
+ }
1115
+
1116
+ fn next_u64 ( & mut self ) -> u64 {
1117
+ self . 0 . next_u64 ( )
1118
+ }
1119
+
1120
+ fn fill_bytes ( & mut self , dest : & mut [ u8 ] ) {
1121
+ self . 0 . fill_bytes ( dest) ;
1122
+ }
1123
+
1124
+ fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
1125
+ self . 0 . try_fill_bytes ( dest)
1126
+ }
1100
1127
}
1101
1128
1129
+ impl SeedableRng for SmallRng {
1130
+ type Seed = <XorShiftRng as SeedableRng >:: Seed ;
1131
+
1132
+ fn from_seed ( seed : Self :: Seed ) -> Self {
1133
+ SmallRng ( XorShiftRng :: from_seed ( seed) )
1134
+ }
1135
+
1136
+ fn from_rng < R : Rng > ( rng : & mut R ) -> Result < Self , Error > {
1137
+ XorShiftRng :: from_rng ( rng) . map ( |rng| SmallRng ( rng) )
1138
+ }
1139
+ }
1102
1140
1103
1141
/// DEPRECATED: use `seq::sample_iter` instead.
1104
1142
///
0 commit comments