Skip to content

Commit ac5f301

Browse files
committed
---
yaml --- r: 145825 b: refs/heads/try2 c: 9886979 h: refs/heads/master i: 145823: 9bc546d v: v3
1 parent b140e8c commit ac5f301

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 0b1a0d01a8d0a769cc2c0bd9b11bfb71864d2f36
8+
refs/heads/try2: 98869799eb2604ecd7c947db117794df10890a2c
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/rand/mod.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ suffice, but sometimes an annotation is required, e.g. `rand::random::<f64>()`.
1919
See the `distributions` submodule for sampling random numbers from
2020
distributions like normal and exponential.
2121
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+
2238
# Examples
2339
2440
```rust
@@ -126,7 +142,7 @@ pub trait Rng {
126142
/// fn main() {
127143
/// let mut v = [0u8, .. 13579];
128144
/// task_rng().fill_bytes(v);
129-
/// printfln!(v);
145+
/// println!("{:?}", v);
130146
/// }
131147
/// ```
132148
fn fill_bytes(&mut self, mut dest: &mut [u8]) {
@@ -486,7 +502,7 @@ pub trait SeedableRng<Seed>: Rng {
486502
/// use std::rand::Rng;
487503
///
488504
/// 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]);
490506
/// println!("{}", rng.gen::<f64>());
491507
/// rng.reseed([5, 6, 7, 8]);
492508
/// println!("{}", rng.gen::<f64>());
@@ -503,7 +519,7 @@ pub trait SeedableRng<Seed>: Rng {
503519
/// use std::rand::Rng;
504520
///
505521
/// 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]);
507523
/// println!("{}", rng.gen::<f64>());
508524
/// }
509525
/// ```
@@ -748,10 +764,10 @@ pub fn task_rng() -> @mut TaskRng {
748764
///
749765
/// fn main() {
750766
/// rand::seed_task_rng(&[10u]);
751-
/// printfln!("Same every time: %u", rand::random::<uint>());
767+
/// println!("Same every time: {}", rand::random::<uint>());
752768
///
753769
/// 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>());
755771
/// }
756772
/// ```
757773
pub fn seed_task_rng(seed: &[uint]) {
@@ -783,9 +799,9 @@ impl<R: Rng> Rng for @mut R {
783799
/// fn main() {
784800
/// if random() {
785801
/// let x = random();
786-
/// printfln!(2u * x);
802+
/// println!("{}", 2u * x);
787803
/// } else {
788-
/// printfln!(random::<float>());
804+
/// println!("{}", random::<float>());
789805
/// }
790806
/// }
791807
/// ```

branches/try2/src/libstd/rand/reader.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! A wrapper around any Reader to treat it as an RNG.
12+
1113
use option::{Some, None};
1214
use rt::io::Reader;
1315
use rt::io::ReaderByteConversions;
@@ -22,12 +24,12 @@ use rand::Rng;
2224
/// # Example
2325
///
2426
/// ```rust
25-
/// use std::rand::reader;
27+
/// use std::rand::{reader, Rng};
2628
/// use std::rt::io::mem;
2729
///
2830
/// fn main() {
2931
/// let mut rng = reader::ReaderRng::new(mem::MemReader::new(~[1,2,3,4,5,6,7,8]));
30-
/// println!("{}", rng.gen::<uint>());
32+
/// println!("{:x}", rng.gen::<uint>());
3133
/// }
3234
/// ```
3335
pub struct ReaderRng<R> {

branches/try2/src/libstd/rand/reseeding.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R> + Default> SeedableRng<S> for Resee
9494
}
9595

9696
/// Something that can be used to reseed an RNG via `ReseedingRng`.
97+
///
98+
/// # Example
99+
///
100+
/// ```rust
101+
/// use std::rand;
102+
/// use std::rand::{Rng, SeedableRng};
103+
/// use std::rand::reseeding::{Reseeder, ReseedingRng};
104+
///
105+
/// struct TickTockReseeder { tick: bool }
106+
/// impl Reseeder<rand::StdRng> for TickTockReseeder {
107+
/// fn reseed(&mut self, rng: &mut rand::StdRng) {
108+
/// let val = if self.tick {0} else {1};
109+
/// rng.reseed(&[val]);
110+
/// self.tick = !self.tick;
111+
/// }
112+
/// }
113+
/// fn main() {
114+
/// let rsdr = TickTockReseeder { tick: true };
115+
/// let mut rng = ReseedingRng::new(rand::StdRng::new(), 10, rsdr);
116+
///
117+
/// // this will repeat, because it gets reseeded very regularly.
118+
/// println(rng.gen_ascii_str(100));
119+
/// }
120+
///
121+
/// ```
97122
pub trait Reseeder<R> {
98123
/// Reseed the given RNG.
99124
fn reseed(&mut self, rng: &mut R);

0 commit comments

Comments
 (0)