@@ -28,13 +28,6 @@ from an operating-system source of randomness, e.g. `/dev/urandom` on
28
28
Unix systems, and will automatically reseed itself from this source
29
29
after generating 32 KiB of random data.
30
30
31
- It can be explicitly seeded on a per-task basis with `seed_task_rng`;
32
- this only affects the task-local generator in the task in which it is
33
- called. It can be seeded globally using the `RUST_SEED` environment
34
- variable, which should be an integer. Setting `RUST_SEED` will seed
35
- every task-local RNG with the same seed. Using either of these will
36
- disable the automatic reseeding.
37
-
38
31
# Examples
39
32
40
33
```rust
@@ -67,7 +60,6 @@ use prelude::*;
67
60
use str;
68
61
use u64;
69
62
use vec;
70
- use os:: getenv;
71
63
72
64
pub use self :: isaac:: { IsaacRng , Isaac64Rng } ;
73
65
pub use self :: os:: OSRng ;
@@ -673,25 +665,11 @@ impl XorShiftRng {
673
665
}
674
666
675
667
/// Controls how the task-local RNG is reseeded.
676
- enum TaskRngReseeder {
677
- /// Reseed using the StdRng::new() function, i.e. reading new
678
- /// randomness.
679
- WithNew,
680
- /// Don't reseed at all, e.g. when it has been explicitly seeded
681
- /// by the user.
682
- DontReseed
683
- }
684
-
685
- impl Default for TaskRngReseeder {
686
- fn default( ) -> TaskRngReseeder { WithNew }
687
- }
668
+ struct TaskRngReseeder ;
688
669
689
670
impl reseeding:: Reseeder < StdRng > for TaskRngReseeder {
690
671
fn reseed ( & mut self , rng : & mut StdRng ) {
691
- match * self {
692
- WithNew => * rng = StdRng :: new ( ) ,
693
- DontReseed => { }
694
- }
672
+ * rng = StdRng :: new ( ) ;
695
673
}
696
674
}
697
675
static TASK_RNG_RESEED_THRESHOLD : uint = 32_768 ;
@@ -706,61 +684,26 @@ local_data_key!(TASK_RNG_KEY: @mut TaskRng)
706
684
/// chaining style, e.g. `task_rng().gen::<int>()`.
707
685
///
708
686
/// The RNG provided will reseed itself from the operating system
709
- /// after generating a certain amount of randomness, unless it was
710
- /// explicitly seeded either by `seed_task_rng` or by setting the
711
- /// `RUST_SEED` environmental variable to some integer.
687
+ /// after generating a certain amount of randomness.
712
688
///
713
- /// The internal RNG used is platform and architecture dependent, so
714
- /// may yield differing sequences on different computers, even when
715
- /// explicitly seeded with `seed_task_rng`. If absolute consistency is
716
- /// required, explicitly select an RNG, e.g. `IsaacRng` or
717
- /// `Isaac64Rng`.
689
+ /// The internal RNG used is platform and architecture dependent, even
690
+ /// if the operating system random number generator is rigged to give
691
+ /// the same sequence always. If absolute consistency is required,
692
+ /// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`.
718
693
pub fn task_rng( ) -> @mut TaskRng {
719
694
let r = local_data:: get( TASK_RNG_KEY , |k| k. map( |& k| * k) ) ;
720
695
match r {
721
696
None => {
722
- // check the environment
723
- let ( sub_rng, reseeder) = match getenv( "RUST_SEED" ) {
724
- None => ( StdRng :: new( ) , WithNew ) ,
725
-
726
- Some ( s) => match from_str:: <uint>( s) {
727
- None => fail2!( "`RUST_SEED` is `{}`, should be a positive integer." , s) ,
728
- // explicitly seeded, so don't overwrite the seed later.
729
- Some ( seed) => ( SeedableRng :: from_seed( & [ seed] ) , DontReseed ) ,
730
- }
731
- } ;
732
-
733
- let rng = @mut reseeding:: ReseedingRng :: new( sub_rng,
697
+ let rng = @mut reseeding:: ReseedingRng :: new( StdRng :: new( ) ,
734
698
TASK_RNG_RESEED_THRESHOLD ,
735
- reseeder ) ;
699
+ TaskRngReseeder ) ;
736
700
local_data:: set( TASK_RNG_KEY , rng) ;
737
701
rng
738
702
}
739
703
Some ( rng) => rng
740
704
}
741
705
}
742
706
743
- /// Explicitly seed (or reseed) the task-local random number
744
- /// generator. This stops the RNG from automatically reseeding itself.
745
- ///
746
- /// # Example
747
- ///
748
- /// ```rust
749
- /// use std::rand;
750
- ///
751
- /// fn main() {
752
- /// rand::seed_task_rng(&[10u]);
753
- /// println!("Same every time: {}", rand::random::<uint>());
754
- ///
755
- /// rand::seed_task_rng(&[1u, 2, 3, 4, 5, 6, 7, 8]);
756
- /// println!("Same every time: {}", rand::random::<float>());
757
- /// }
758
- /// ```
759
- pub fn seed_task_rng( seed: & [ uint] ) {
760
- let t_r = task_rng( ) ;
761
- ( * t_r) . reseed( ( DontReseed , seed) ) ;
762
- }
763
-
764
707
// Allow direct chaining with `task_rng`
765
708
impl <R : Rng > Rng for @mut R {
766
709
#[ inline]
@@ -996,16 +939,6 @@ mod test {
996
939
let string2 = r. gen_ascii_str( 100 ) ;
997
940
assert_eq!( string1, string2) ;
998
941
}
999
-
1000
- #[ test]
1001
- fn test_seed_task_rng( ) {
1002
- seed_task_rng( [ 1 ] ) ;
1003
- let first = random:: <uint>( ) ;
1004
-
1005
- seed_task_rng( [ 1 ] ) ;
1006
- let second = random:: <uint>( ) ;
1007
- assert_eq!( first, second) ;
1008
- }
1009
942
}
1010
943
1011
944
#[ cfg( test) ]
0 commit comments