@@ -276,6 +276,7 @@ use prng::Isaac64Rng as IsaacWordRng;
276
276
277
277
use distributions:: { Range , IndependentSample } ;
278
278
use distributions:: range:: SampleRange ;
279
+ use reseeding:: { ReseedingRng , ReseedWithNew } ;
279
280
280
281
// public modules
281
282
pub mod distributions;
@@ -933,58 +934,39 @@ pub fn weak_rng() -> XorShiftRng {
933
934
XorShiftRng :: new ( ) . unwrap ( )
934
935
}
935
936
936
- /// Controls how the thread-local RNG is reseeded.
937
- #[ cfg( feature="std" ) ]
938
- #[ derive( Debug ) ]
939
- struct ThreadRngReseeder ;
940
-
941
- #[ cfg( feature="std" ) ]
942
- impl reseeding:: Reseeder < StdRng > for ThreadRngReseeder {
943
- fn reseed ( & mut self , rng : & mut StdRng ) {
944
- match StdRng :: new ( ) {
945
- Ok ( r) => * rng = r,
946
- Err ( e) => panic ! ( "No entropy available: {}" , e) ,
947
- }
948
- }
949
- }
950
- #[ cfg( feature="std" ) ]
951
- const THREAD_RNG_RESEED_THRESHOLD : u64 = 32_768 ;
952
- #[ cfg( feature="std" ) ]
953
- type ThreadRngInner = reseeding:: ReseedingRng < StdRng , ThreadRngReseeder > ;
954
937
955
938
/// The thread-local RNG.
956
939
#[ cfg( feature="std" ) ]
957
940
#[ derive( Clone , Debug ) ]
958
941
pub struct ThreadRng {
959
- rng : Rc < RefCell < ThreadRngInner > > ,
942
+ rng : Rc < RefCell < ReseedingRng < StdRng , ReseedWithNew > > > ,
960
943
}
961
944
962
- /// Retrieve the lazily-initialized thread-local random number
963
- /// generator, seeded by the system. Intended to be used in method
964
- /// chaining style, e.g. `thread_rng().gen::<i32>()`.
965
- ///
966
- /// After generating a certain amount of randomness, the RNG will reseed itself
967
- /// from the operating system or, if the operating system RNG returns an error,
968
- /// a seed based on the current system time.
969
- ///
970
- /// The internal RNG used is platform and architecture dependent, even
971
- /// if the operating system random number generator is rigged to give
972
- /// the same sequence always. If absolute consistency is required,
973
- /// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`.
974
945
#[ cfg( feature="std" ) ]
975
- pub fn thread_rng ( ) -> ThreadRng {
976
- // used to make space in TLS for a random number generator
977
- thread_local ! ( static THREAD_RNG_KEY : Rc < RefCell < ThreadRngInner >> = {
946
+ thread_local ! (
947
+ static THREAD_RNG_KEY : Rc < RefCell < ReseedingRng < StdRng , ReseedWithNew >>> = {
948
+ const THREAD_RNG_RESEED_THRESHOLD : u64 = 32_768 ;
978
949
let r = match StdRng :: new( ) {
979
950
Ok ( r) => r,
980
- Err ( e) => panic!( "No entropy available : {}" , e) ,
951
+ Err ( e) => panic!( "could not initialize thread_rng : {:? }" , e)
981
952
} ;
982
- let rng = reseeding :: ReseedingRng :: new( r,
983
- THREAD_RNG_RESEED_THRESHOLD ,
984
- ThreadRngReseeder ) ;
953
+ let rng = ReseedingRng :: new( r,
954
+ THREAD_RNG_RESEED_THRESHOLD ,
955
+ ReseedWithNew ) ;
985
956
Rc :: new( RefCell :: new( rng) )
986
- } ) ;
957
+ }
958
+ ) ;
987
959
960
+ /// Retrieve the lazily-initialized thread-local random number
961
+ /// generator, seeded by the system. Intended to be used in method
962
+ /// chaining style, e.g. `thread_rng().gen::<i32>()`.
963
+ ///
964
+ /// The internal RNG used is the one defined by `StdRng`. After generating 32KiB
965
+ /// of random bytes, the RNG will reseed itself from the operating system or, if
966
+ /// the operating system RNG returns an error, the `JitterRng` entropy
967
+ /// collector.
968
+ #[ cfg( feature="std" ) ]
969
+ pub fn thread_rng ( ) -> ThreadRng {
988
970
ThreadRng { rng : THREAD_RNG_KEY . with ( |t| t. clone ( ) ) }
989
971
}
990
972
0 commit comments