@@ -19,6 +19,22 @@ suffice, but sometimes an annotation is required, e.g. `rand::random::<f64>()`.
19
19
See the `distributions` submodule for sampling random numbers from
20
20
distributions like normal and exponential.
21
21
22
+ # Task-local RNG
23
+
24
+ There is built-in support for a RNG associated with each task stored
25
+ in task-local storage. This RNG can be accessed via `task_rng`, or
26
+ used implicitly via `random`. This RNG is normally randomly seeded
27
+ from an operating-system source of randomness, e.g. `/dev/urandom` on
28
+ Unix systems, and will automatically reseed itself from this source
29
+ after generating 32 KiB of random data.
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
+
22
38
# Examples
23
39
24
40
```rust
@@ -126,7 +142,7 @@ pub trait Rng {
126
142
/// fn main() {
127
143
/// let mut v = [0u8, .. 13579];
128
144
/// task_rng().fill_bytes(v);
129
- /// printfln!( v);
145
+ /// println!("{:?}", v);
130
146
/// }
131
147
/// ```
132
148
fn fill_bytes ( & mut self , mut dest : & mut [ u8 ] ) {
@@ -486,7 +502,7 @@ pub trait SeedableRng<Seed>: Rng {
486
502
/// use std::rand::Rng;
487
503
///
488
504
/// fn main() {
489
- /// let mut rng: rand::XorShiftRng = rand::SeedableRng::from_seed(&[1, 2, 3, 4]);
505
+ /// let mut rng: rand::StdRng = rand::SeedableRng::from_seed(&[1, 2, 3, 4]);
490
506
/// println!("{}", rng.gen::<f64>());
491
507
/// rng.reseed([5, 6, 7, 8]);
492
508
/// println!("{}", rng.gen::<f64>());
@@ -503,7 +519,7 @@ pub trait SeedableRng<Seed>: Rng {
503
519
/// use std::rand::Rng;
504
520
///
505
521
/// fn main() {
506
- /// let mut rng: rand::XorShiftRng = rand::SeedableRng::from_seed(&[1, 2, 3, 4]);
522
+ /// let mut rng: rand::StdRng = rand::SeedableRng::from_seed(&[1, 2, 3, 4]);
507
523
/// println!("{}", rng.gen::<f64>());
508
524
/// }
509
525
/// ```
@@ -748,10 +764,10 @@ pub fn task_rng() -> @mut TaskRng {
748
764
///
749
765
/// fn main() {
750
766
/// rand::seed_task_rng(&[10u]);
751
- /// printfln !("Same every time: %u ", rand::random::<uint>());
767
+ /// println !("Same every time: {} ", rand::random::<uint>());
752
768
///
753
769
/// rand::seed_task_rng(&[1u, 2, 3, 4, 5, 6, 7, 8]);
754
- /// printfln !("Same every time: %f ", rand::random::<float>());
770
+ /// println !("Same every time: {} ", rand::random::<float>());
755
771
/// }
756
772
/// ```
757
773
pub fn seed_task_rng ( seed : & [ uint ] ) {
@@ -783,9 +799,9 @@ impl<R: Rng> Rng for @mut R {
783
799
/// fn main() {
784
800
/// if random() {
785
801
/// let x = random();
786
- /// printfln!( 2u * x);
802
+ /// println!("{}", 2u * x);
787
803
/// } else {
788
- /// printfln!( random::<float>());
804
+ /// println!("{}", random::<float>());
789
805
/// }
790
806
/// }
791
807
/// ```
0 commit comments