Skip to content

Commit 0cc7a31

Browse files
committed
---
yaml --- r: 93183 b: refs/heads/try c: e0eb128 h: refs/heads/master i: 93181: 2792437 93179: 2eb8372 93175: d219cf1 93167: bf5d208 93151: 09a07bc 93119: acef17f 93055: 2423453 92927: da2ec00 92671: b5e3d83 92159: 166798c v: v3
1 parent e6a123b commit 0cc7a31

File tree

6 files changed

+103
-45
lines changed

6 files changed

+103
-45
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0da105a8b7b6b1e0568e8ff20f6ff4b13cc7ecc2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d3e57dca68fde4effdda3e4ae2887aa535fcd6
5-
refs/heads/try: 148f737c199a5c9dd6d349751072add3cc458533
5+
refs/heads/try: e0eb1280867e14bdb123c3b19eda93b8906899d2
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/rand/distributions.rs

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

11-
//! Sampling from random distributions
11+
/*!
12+
Sampling from random distributions.
1213
13-
// Some implementations use the Ziggurat method
14-
// https://en.wikipedia.org/wiki/Ziggurat_algorithm
15-
//
16-
// The version used here is ZIGNOR [Doornik 2005, "An Improved
17-
// Ziggurat Method to Generate Normal Random Samples"] which is slower
18-
// (about double, it generates an extra random number) than the
19-
// canonical version [Marsaglia & Tsang 2000, "The Ziggurat Method for
20-
// Generating Random Variables"], but more robust. If one wanted, one
21-
// could implement VIZIGNOR the ZIGNOR paper for more speed.
14+
This is a generalization of `Rand` to allow parameters to control the
15+
exact properties of the generated values, e.g. the mean and standard
16+
deviation of a normal distribution. The `Sample` trait is the most
17+
general, and allows for generating values that change some state
18+
internally. The `IndependentSample` trait is for generating values
19+
that do not need to record state.
20+
21+
*/
2222

2323
use num;
2424
use rand::{Rng,Rand};
@@ -27,16 +27,18 @@ pub use self::range::Range;
2727

2828
pub mod range;
2929

30-
/// Things that can be used to create a random instance of `Support`.
30+
/// Types that can be used to create a random instance of `Support`.
3131
pub trait Sample<Support> {
3232
/// Generate a random value of `Support`, using `rng` as the
3333
/// source of randomness.
3434
fn sample<R: Rng>(&mut self, rng: &mut R) -> Support;
3535
}
3636

37-
/// `Sample`s that do not require keeping track of state, so each
38-
/// sample is (statistically) independent of all others, assuming the
39-
/// `Rng` used has this property.
37+
/// `Sample`s that do not require keeping track of state.
38+
///
39+
/// Since no state is recored, each sample is (statistically)
40+
/// independent of all others, assuming the `Rng` used has this
41+
/// property.
4042
// XXX maybe having this separate is overkill (the only reason is to
4143
// take &self rather than &mut self)? or maybe this should be the
4244
// trait called `Sample` and the other should be `DependentSample`.
@@ -91,13 +93,19 @@ fn ziggurat<R:Rng>(rng: &mut R,
9193
}
9294
}
9395

94-
/// A wrapper around an `f64` to generate N(0, 1) random numbers (a.k.a. a
95-
/// standard normal, or Gaussian). Multiplying the generated values by the
96-
/// desired standard deviation `sigma` then adding the desired mean `mu` will
97-
/// give N(mu, sigma^2) distributed random numbers.
96+
/// A wrapper around an `f64` to generate N(0, 1) random numbers
97+
/// (a.k.a. a standard normal, or Gaussian).
9898
///
99-
/// Note that this has to be unwrapped before use as an `f64` (using either
100-
/// `*` or `cast::transmute` is safe).
99+
/// See `Normal` for the general normal distribution. That this has to
100+
/// be unwrapped before use as an `f64` (using either `*` or
101+
/// `cast::transmute` is safe).
102+
///
103+
/// Implemented via the ZIGNOR variant[1] of the Ziggurat method.
104+
///
105+
/// [1]: Jurgen A. Doornik (2005). [*An Improved Ziggurat Method to
106+
/// Generate Normal Random
107+
/// Samples*](http://www.doornik.com/research/ziggurat.pdf). Nuffield
108+
/// College, Oxford
101109
pub struct StandardNormal(f64);
102110

103111
impl Rand for StandardNormal {
@@ -135,8 +143,10 @@ impl Rand for StandardNormal {
135143
}
136144
}
137145

138-
/// The `N(mean, std_dev**2)` distribution, i.e. samples from a normal
139-
/// distribution with mean `mean` and standard deviation `std_dev`.
146+
/// The normal distribution `N(mean, std_dev**2)`.
147+
///
148+
/// This uses the ZIGNOR variant of the Ziggurat method, see
149+
/// `StandardNormal` for more details.
140150
///
141151
/// # Example
142152
///
@@ -175,12 +185,20 @@ impl IndependentSample<f64> for Normal {
175185
}
176186
}
177187

178-
/// A wrapper around an `f64` to generate Exp(1) random numbers. Dividing by
179-
/// the desired rate `lambda` will give Exp(lambda) distributed random
180-
/// numbers.
188+
/// A wrapper around an `f64` to generate Exp(1) random numbers.
181189
///
182-
/// Note that this has to be unwrapped before use as an `f64` (using either
190+
/// See `Exp` for the general exponential distribution.Note that this
191+
// has to be unwrapped before use as an `f64` (using either
183192
/// `*` or `cast::transmute` is safe).
193+
///
194+
/// Implemented via the ZIGNOR variant[1] of the Ziggurat method. The
195+
/// exact description in the paper was adjusted to use tables for the
196+
/// exponential distribution rather than normal.
197+
///
198+
/// [1]: Jurgen A. Doornik (2005). [*An Improved Ziggurat Method to
199+
/// Generate Normal Random
200+
/// Samples*](http://www.doornik.com/research/ziggurat.pdf). Nuffield
201+
/// College, Oxford
184202
pub struct Exp1(f64);
185203

186204
// This could be done via `-rng.gen::<f64>().ln()` but that is slower.
@@ -203,8 +221,7 @@ impl Rand for Exp1 {
203221
}
204222
}
205223

206-
/// The `Exp(lambda)` distribution; i.e. samples from the exponential
207-
/// distribution with rate parameter `lambda`.
224+
/// The exponential distribution `Exp(lambda)`.
208225
///
209226
/// This distribution has density function: `f(x) = lambda *
210227
/// exp(-lambda * x)` for `x > 0`.

branches/try/src/libstd/rand/isaac.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ use option::{None, Some};
1818
static RAND_SIZE_LEN: u32 = 8;
1919
static RAND_SIZE: u32 = 1 << RAND_SIZE_LEN;
2020

21-
/// A random number generator that uses the [ISAAC
22-
/// algorithm](http://en.wikipedia.org/wiki/ISAAC_%28cipher%29).
21+
/// A random number generator that uses the ISAAC algorithm[1].
2322
///
24-
/// The ISAAC algorithm is suitable for cryptographic purposes.
23+
/// The ISAAC algorithm is generally accepted as suitable for
24+
/// cryptographic purposes, but this implementation has not be
25+
/// verified as such. Prefer a generator like `OSRng` that defers to
26+
/// the operating system for cases that need high security.
27+
///
28+
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
29+
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
2530
pub struct IsaacRng {
2631
priv cnt: u32,
2732
priv rsl: [u32, .. RAND_SIZE],
@@ -212,11 +217,16 @@ impl<'self> SeedableRng<&'self [u32]> for IsaacRng {
212217
static RAND_SIZE_64_LEN: uint = 8;
213218
static RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
214219

215-
/// A random number generator that uses the 64-bit variant of the
216-
/// [ISAAC
217-
/// algorithm](http://en.wikipedia.org/wiki/ISAAC_%28cipher%29).
220+
/// A random number generator that uses ISAAC-64[1], the 64-bit
221+
/// variant of the ISAAC algorithm.
222+
///
223+
/// The ISAAC algorithm is generally accepted as suitable for
224+
/// cryptographic purposes, but this implementation has not be
225+
/// verified as such. Prefer a generator like `OSRng` that defers to
226+
/// the operating system for cases that need high security.
218227
///
219-
/// The ISAAC algorithm is suitable for cryptographic purposes.
228+
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
229+
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
220230
pub struct Isaac64Rng {
221231
priv cnt: uint,
222232
priv rsl: [u64, .. RAND_SIZE_64],

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ from an operating-system source of randomness, e.g. `/dev/urandom` on
2828
Unix systems, and will automatically reseed itself from this source
2929
after generating 32 KiB of random data.
3030
31+
# Cryptographic security
32+
33+
An application that requires random numbers for cryptographic purposes
34+
should prefer `OSRng`, which reads randomness from one of the source
35+
that the operating system provides (e.g. `/dev/urandom` on
36+
Unixes). The other random number generators provided by this module
37+
are either known to be insecure (`XorShiftRng`), or are not verified
38+
to be secure (`IsaacRng`, `Isaac64Rng` and `StdRng`).
39+
40+
*Note*: on Linux, `/dev/random` is more secure than `/dev/urandom`,
41+
but it is a blocking RNG, and will wait until it has determined that
42+
it has collected enough entropy to fulfill a request for random
43+
data. It can be used with the `Rng` trait provided by this module by
44+
opening the file and passing it to `reader::ReaderRng`. Since it
45+
blocks, `/dev/random` should only be used to retrieve small amounts of
46+
randomness.
47+
3148
# Examples
3249
3350
```rust
@@ -516,8 +533,8 @@ pub trait SeedableRng<Seed>: Rng {
516533

517534
/// Create a random number generator with a default algorithm and seed.
518535
///
519-
/// It returns the cryptographically-safest `Rng` algorithm currently
520-
/// available in Rust. If you require a specifically seeded `Rng` for
536+
/// It returns the strongest `Rng` algorithm currently implemented in
537+
/// pure Rust. If you require a specifically seeded `Rng` for
521538
/// consistency over time you should pick one algorithm and create the
522539
/// `Rng` yourself.
523540
///
@@ -592,12 +609,16 @@ pub fn weak_rng() -> XorShiftRng {
592609
XorShiftRng::new()
593610
}
594611

595-
/// An [Xorshift random number
596-
/// generator](http://en.wikipedia.org/wiki/Xorshift).
612+
/// An Xorshift[1] random number
613+
/// generator.
597614
///
598615
/// The Xorshift algorithm is not suitable for cryptographic purposes
599616
/// but is very fast. If you do not know for sure that it fits your
600-
/// requirements, use a more secure one such as `IsaacRng`.
617+
/// requirements, use a more secure one such as `IsaacRng` or `OSRng`.
618+
///
619+
/// [1]: Marsaglia, George (July 2003). ["Xorshift
620+
/// RNGs"](http://www.jstatsoft.org/v08/i14/paper). *Journal of
621+
/// Statistical Software*. Vol. 8 (Issue 14).
601622
pub struct XorShiftRng {
602623
priv x: u32,
603624
priv y: u32,

branches/try/src/libstd/rand/os.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,25 @@ type HCRYPTPROV = c_long;
3030
// assume they work when we call them.
3131

3232
/// A random number generator that retrieves randomness straight from
33-
/// the operating system. On Unix-like systems this reads from
34-
/// `/dev/urandom`, on Windows this uses `CryptGenRandom`.
33+
/// the operating system. Platform sources:
34+
///
35+
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
36+
/// `/dev/urandom`.
37+
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
38+
/// service provider with the `PROV_RSA_FULL` type.
3539
///
3640
/// This does not block.
3741
#[cfg(unix)]
3842
pub struct OSRng {
3943
priv inner: ReaderRng<file::FileStream>
4044
}
4145
/// A random number generator that retrieves randomness straight from
42-
/// the operating system. On Unix-like systems this reads from
43-
/// `/dev/urandom`, on Windows this uses `CryptGenRandom`.
46+
/// the operating system. Platform sources:
47+
///
48+
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
49+
/// `/dev/urandom`.
50+
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
51+
/// service provider with the `PROV_RSA_FULL` type.
4452
///
4553
/// This does not block.
4654
#[cfg(windows)]

branches/try/src/rt/rust_builtin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,8 @@ extern "C" CDECL void
672672
rust_win32_rand_acquire(HCRYPTPROV* phProv) {
673673
win32_require
674674
(_T("CryptAcquireContext"),
675+
// changes to the parameters here should be reflected in the docs of
676+
// std::rand::os::OSRng
675677
CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL,
676678
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
677679

0 commit comments

Comments
 (0)