@@ -5,10 +5,8 @@ use crate::Rng;
5
5
use std:: cell:: Cell ;
6
6
use std:: ops:: RangeBounds ;
7
7
8
- #[ cfg( all( target_arch = "wasm32" , not( target_os = "wasi" ) ) ) ]
9
- use instant:: Instant ;
10
- #[ cfg( not( all( target_arch = "wasm32" , not( target_os = "wasi" ) ) ) ) ]
11
- use std:: time:: Instant ;
8
+ // Chosen by fair roll of the dice.
9
+ const DEFAULT_RNG_SEED : u64 = 0xef6f79ed30ba75a ;
12
10
13
11
impl Default for Rng {
14
12
/// Initialize the `Rng` from the system's random number generator.
@@ -29,17 +27,7 @@ impl Rng {
29
27
}
30
28
31
29
thread_local ! {
32
- static RNG : Cell <Rng > = Cell :: new( Rng ( {
33
- use std:: collections:: hash_map:: DefaultHasher ;
34
- use std:: hash:: { Hash , Hasher } ;
35
- use std:: thread;
36
-
37
- let mut hasher = DefaultHasher :: new( ) ;
38
- Instant :: now( ) . hash( & mut hasher) ;
39
- thread:: current( ) . id( ) . hash( & mut hasher) ;
40
- let hash = hasher. finish( ) ;
41
- ( hash << 1 ) | 1
42
- } ) ) ;
30
+ static RNG : Cell <Rng > = Cell :: new( Rng ( random_seed( ) . unwrap_or( DEFAULT_RNG_SEED ) ) ) ;
43
31
}
44
32
45
33
/// Run an operation with the current thread-local generator.
@@ -190,3 +178,41 @@ pub fn f64() -> f64 {
190
178
pub fn choose_multiple < T : Iterator > ( source : T , amount : usize ) -> Vec < T :: Item > {
191
179
with_rng ( |rng| rng. choose_multiple ( source, amount) )
192
180
}
181
+
182
+ #[ cfg( not( all(
183
+ any( target_arch = "wasm32" , target_arch = "wasm64" ) ,
184
+ target_os = "unknown"
185
+ ) ) ) ]
186
+ fn random_seed ( ) -> Option < u64 > {
187
+ use std:: collections:: hash_map:: DefaultHasher ;
188
+ use std:: hash:: { Hash , Hasher } ;
189
+ use std:: thread;
190
+ use std:: time:: Instant ;
191
+
192
+ let mut hasher = DefaultHasher :: new ( ) ;
193
+ Instant :: now ( ) . hash ( & mut hasher) ;
194
+ thread:: current ( ) . id ( ) . hash ( & mut hasher) ;
195
+ let hash = hasher. finish ( ) ;
196
+ Some ( ( hash << 1 ) | 1 )
197
+ }
198
+
199
+ #[ cfg( all(
200
+ any( target_arch = "wasm32" , target_arch = "wasm64" ) ,
201
+ target_os = "unknown" ,
202
+ feature = "js"
203
+ ) ) ]
204
+ fn random_seed ( ) -> Option < u64 > {
205
+ // TODO(notgull): Failures should be logged somewhere.
206
+ let mut seed = [ 0u8 ; 8 ] ;
207
+ getrandom:: getrandom ( & mut seed) . ok ( ) ?;
208
+ Some ( u64:: from_ne_bytes ( seed) )
209
+ }
210
+
211
+ #[ cfg( all(
212
+ any( target_arch = "wasm32" , target_arch = "wasm64" ) ,
213
+ target_os = "unknown" ,
214
+ not( feature = "js" )
215
+ ) ) ]
216
+ fn random_seed ( ) -> Option < u64 > {
217
+ None
218
+ }
0 commit comments