std: Move rand to librand.

This functionality is not super-core and so doesn't need to be included
in std. It's possible that std may need rand (it does a little bit now,
for io::test) in which case the functionality required could be moved to
a secret hidden module and reexposed by librand.

Unfortunately, using #[deprecated] here is hard: there's too much to
mock to make it feasible, since we have to ensure that programs still
typecheck to reach the linting phase.
This commit is contained in:
Huon Wilson 2014-03-02 11:23:04 +11:00
parent 74bfa7108a
commit 6fa4bbeed4
18 changed files with 161 additions and 198 deletions

View File

@ -50,14 +50,14 @@
################################################################################ ################################################################################
TARGET_CRATES := std extra green rustuv native flate arena glob term semver \ TARGET_CRATES := std extra green rustuv native flate arena glob term semver \
uuid serialize sync getopts collections num test time uuid serialize sync getopts collections num test time rand
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat HOST_CRATES := syntax rustc rustdoc fourcc hexfloat
CRATES := $(TARGET_CRATES) $(HOST_CRATES) CRATES := $(TARGET_CRATES) $(HOST_CRATES)
TOOLS := compiletest rustdoc rustc TOOLS := compiletest rustdoc rustc
DEPS_std := native:rustrt native:compiler-rt DEPS_std := native:rustrt native:compiler-rt
DEPS_extra := std term sync serialize getopts collections time DEPS_extra := std term sync serialize getopts collections time rand
DEPS_green := std native:context_switch DEPS_green := std rand native:context_switch
DEPS_rustuv := std native:uv native:uv_support DEPS_rustuv := std native:uv native:uv_support
DEPS_native := std DEPS_native := std
DEPS_syntax := std term serialize collections DEPS_syntax := std term serialize collections
@ -71,15 +71,16 @@ DEPS_glob := std
DEPS_serialize := std collections DEPS_serialize := std collections
DEPS_term := std collections DEPS_term := std collections
DEPS_semver := std DEPS_semver := std
DEPS_uuid := std serialize DEPS_uuid := std serialize rand
DEPS_sync := std DEPS_sync := std
DEPS_getopts := std DEPS_getopts := std
DEPS_collections := std DEPS_collections := std rand
DEPS_fourcc := syntax std DEPS_fourcc := syntax std
DEPS_hexfloat := syntax std DEPS_hexfloat := syntax std
DEPS_num := std DEPS_num := std rand
DEPS_test := std extra collections getopts serialize term DEPS_test := std extra collections getopts serialize term
DEPS_time := std serialize DEPS_time := std serialize
DEPS_rand := std
TOOL_DEPS_compiletest := test green rustuv getopts TOOL_DEPS_compiletest := test green rustuv getopts
TOOL_DEPS_rustdoc := rustdoc native TOOL_DEPS_rustdoc := rustdoc native

View File

@ -11,7 +11,7 @@
# except according to those terms. # except according to those terms.
# This creates the tables used for distributions implemented using the # This creates the tables used for distributions implemented using the
# ziggurat algorithm in `std::rand::distributions;`. They are # ziggurat algorithm in `rand::distributions;`. They are
# (basically) the tables as used in the ZIGNOR variant (Doornik 2005). # (basically) the tables as used in the ZIGNOR variant (Doornik 2005).
# They are changed rarely, so the generated file should be checked in # They are changed rarely, so the generated file should be checked in
# to git. # to git.

View File

@ -10,9 +10,9 @@
//! The exponential distribution. //! The exponential distribution.
use num::Float; use std::num::Float;
use rand::{Rng, Rand}; use {Rng, Rand};
use rand::distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample}; use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
/// A wrapper around an `f64` to generate Exp(1) random numbers. /// A wrapper around an `f64` to generate Exp(1) random numbers.
/// ///
@ -58,8 +58,7 @@ impl Rand for Exp1 {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{Exp, IndependentSample};
/// use std::rand::distributions::{Exp, IndependentSample};
/// ///
/// let exp = Exp::new(2.0); /// let exp = Exp::new(2.0);
/// let v = exp.ind_sample(&mut rand::task_rng()); /// let v = exp.ind_sample(&mut rand::task_rng());
@ -91,10 +90,9 @@ impl IndependentSample<f64> for Exp {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use rand::distributions::*; use distributions::{Sample, IndependentSample};
use prelude::*; use {Rng, task_rng};
use rand::*; use super::Exp;
use super::*;
#[test] #[test]
fn test_exp() { fn test_exp() {
@ -121,11 +119,10 @@ mod test {
mod bench { mod bench {
extern crate test; extern crate test;
use self::test::BenchHarness; use self::test::BenchHarness;
use mem::size_of; use std::mem::size_of;
use prelude::*; use {XorShiftRng, RAND_BENCH_N};
use rand::{XorShiftRng, RAND_BENCH_N}; use super::Exp;
use super::*; use distributions::Sample;
use rand::distributions::*;
#[bench] #[bench]
fn rand_exp(bh: &mut BenchHarness) { fn rand_exp(bh: &mut BenchHarness) {

View File

@ -10,9 +10,9 @@
//! The Gamma and derived distributions. //! The Gamma and derived distributions.
use num::Float; use std::num::Float;
use num; use std::num;
use rand::{Rng, Open01}; use {Rng, Open01};
use super::normal::StandardNormal; use super::normal::StandardNormal;
use super::{IndependentSample, Sample, Exp}; use super::{IndependentSample, Sample, Exp};
@ -20,7 +20,7 @@ use super::{IndependentSample, Sample, Exp};
/// ///
/// The density function of this distribution is /// The density function of this distribution is
/// ///
/// ```ignore /// ```notrust
/// f(x) = x^(k - 1) * exp(-x / θ) / (Γ(k) * θ^k) /// f(x) = x^(k - 1) * exp(-x / θ) / (Γ(k) * θ^k)
/// ``` /// ```
/// ///
@ -35,8 +35,7 @@ use super::{IndependentSample, Sample, Exp};
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{IndependentSample, Gamma};
/// use std::rand::distributions::{IndependentSample, Gamma};
/// ///
/// let gamma = Gamma::new(2.0, 5.0); /// let gamma = Gamma::new(2.0, 5.0);
/// let v = gamma.ind_sample(&mut rand::task_rng()); /// let v = gamma.ind_sample(&mut rand::task_rng());
@ -179,8 +178,7 @@ impl IndependentSample<f64> for GammaLargeShape {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{ChiSquared, IndependentSample};
/// use std::rand::distributions::{ChiSquared, IndependentSample};
/// ///
/// let chi = ChiSquared::new(11.0); /// let chi = ChiSquared::new(11.0);
/// let v = chi.ind_sample(&mut rand::task_rng()); /// let v = chi.ind_sample(&mut rand::task_rng());
@ -231,8 +229,7 @@ impl IndependentSample<f64> for ChiSquared {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{FisherF, IndependentSample};
/// use std::rand::distributions::{FisherF, IndependentSample};
/// ///
/// let f = FisherF::new(2.0, 32.0); /// let f = FisherF::new(2.0, 32.0);
/// let v = f.ind_sample(&mut rand::task_rng()); /// let v = f.ind_sample(&mut rand::task_rng());
@ -275,8 +272,7 @@ impl IndependentSample<f64> for FisherF {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{StudentT, IndependentSample};
/// use std::rand::distributions::{StudentT, IndependentSample};
/// ///
/// let t = StudentT::new(11.0); /// let t = StudentT::new(11.0);
/// let v = t.ind_sample(&mut rand::task_rng()); /// let v = t.ind_sample(&mut rand::task_rng());
@ -310,10 +306,9 @@ impl IndependentSample<f64> for StudentT {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use rand::distributions::*; use distributions::{Sample, IndependentSample};
use prelude::*; use {Rng, task_rng};
use rand::*; use super::{ChiSquared, StudentT, FisherF};
use super::*;
#[test] #[test]
fn test_chi_squared_one() { fn test_chi_squared_one() {
@ -344,7 +339,7 @@ mod test {
} }
#[test] #[test]
#[should_fail] #[should_fail]
fn test_log_normal_invalid_dof() { fn test_chi_squared_invalid_dof() {
ChiSquared::new(-1.0); ChiSquared::new(-1.0);
} }
@ -373,11 +368,10 @@ mod test {
mod bench { mod bench {
extern crate test; extern crate test;
use self::test::BenchHarness; use self::test::BenchHarness;
use mem::size_of; use std::mem::size_of;
use prelude::*; use distributions::IndependentSample;
use rand::distributions::IndependentSample; use {StdRng, RAND_BENCH_N};
use rand::{StdRng, RAND_BENCH_N}; use super::Gamma;
use super::*;
#[bench] #[bench]

View File

@ -20,14 +20,9 @@ that do not need to record state.
*/ */
use container::Container; use std::num;
use iter::{range, Iterator}; use std::num::CheckedAdd;
use option::{Some, None}; use {Rng, Rand};
use num;
use num::CheckedAdd;
use rand::{Rng, Rand};
use clone::Clone;
use vec::MutableVector;
pub use self::range::Range; pub use self::range::Range;
pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT}; pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT};
@ -94,8 +89,7 @@ pub struct Weighted<T> {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{Weighted, WeightedChoice, IndependentSample};
/// use std::rand::distributions::{Weighted, WeightedChoice, IndependentSample};
/// ///
/// let wc = WeightedChoice::new(~[Weighted { weight: 2, item: 'a' }, /// let wc = WeightedChoice::new(~[Weighted { weight: 2, item: 'a' },
/// Weighted { weight: 4, item: 'b' }, /// Weighted { weight: 4, item: 'b' },
@ -253,9 +247,8 @@ fn ziggurat<R:Rng>(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use prelude::*; use {task_rng, Rng, Rand};
use rand::*; use super::{RandSample, WeightedChoice, Weighted, Sample, IndependentSample};
use super::*;
#[deriving(Eq, Show)] #[deriving(Eq, Show)]
struct ConstRand(uint); struct ConstRand(uint);

View File

@ -10,9 +10,9 @@
//! The normal and derived distributions. //! The normal and derived distributions.
use num::Float; use std::num::Float;
use rand::{Rng, Rand, Open01}; use {Rng, Rand, Open01};
use rand::distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample}; use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
/// A wrapper around an `f64` to generate N(0, 1) random numbers /// A wrapper around an `f64` to generate N(0, 1) random numbers
/// (a.k.a. a standard normal, or Gaussian). /// (a.k.a. a standard normal, or Gaussian).
@ -74,8 +74,7 @@ impl Rand for StandardNormal {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{Normal, IndependentSample};
/// use std::rand::distributions::{Normal, IndependentSample};
/// ///
/// // mean 2, standard deviation 3 /// // mean 2, standard deviation 3
/// let normal = Normal::new(2.0, 3.0); /// let normal = Normal::new(2.0, 3.0);
@ -117,8 +116,7 @@ impl IndependentSample<f64> for Normal {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{LogNormal, IndependentSample};
/// use std::rand::distributions::{LogNormal, IndependentSample};
/// ///
/// // mean 2, standard deviation 3 /// // mean 2, standard deviation 3
/// let log_normal = LogNormal::new(2.0, 3.0); /// let log_normal = LogNormal::new(2.0, 3.0);
@ -148,10 +146,9 @@ impl IndependentSample<f64> for LogNormal {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use prelude::*; use distributions::{Sample, IndependentSample};
use rand::*; use {Rng, task_rng};
use super::*; use super::{Normal, LogNormal};
use rand::distributions::*;
#[test] #[test]
fn test_normal() { fn test_normal() {
@ -189,11 +186,10 @@ mod tests {
mod bench { mod bench {
extern crate test; extern crate test;
use self::test::BenchHarness; use self::test::BenchHarness;
use mem::size_of; use std::mem::size_of;
use prelude::*; use {XorShiftRng, RAND_BENCH_N};
use rand::{XorShiftRng, RAND_BENCH_N}; use distributions::{Sample};
use rand::distributions::*; use super::Normal;
use super::*;
#[bench] #[bench]
fn rand_normal(bh: &mut BenchHarness) { fn rand_normal(bh: &mut BenchHarness) {

View File

@ -12,10 +12,9 @@
// this is surprisingly complicated to be both generic & correct // this is surprisingly complicated to be both generic & correct
use cmp::Ord; use std::num::Bounded;
use num::Bounded; use Rng;
use rand::Rng; use distributions::{Sample, IndependentSample};
use rand::distributions::{Sample, IndependentSample};
/// Sample values uniformly between two bounds. /// Sample values uniformly between two bounds.
/// ///
@ -34,8 +33,7 @@ use rand::distributions::{Sample, IndependentSample};
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::distributions::{IndependentSample, Range};
/// use std::rand::distributions::{IndependentSample, Range};
/// ///
/// fn main() { /// fn main() {
/// let between = Range::new(10u, 10000u); /// let between = Range::new(10u, 10000u);
@ -163,11 +161,10 @@ float_impl! { f64 }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use prelude::*; use distributions::{Sample, IndependentSample};
use super::*; use {Rng, task_rng};
use rand::*; use super::Range;
use rand::distributions::*; use std::num::Bounded;
use num::Bounded;
#[should_fail] #[should_fail]
#[test] #[test]

View File

@ -10,11 +10,10 @@
//! The ISAAC random number generator. //! The ISAAC random number generator.
use rand::{Rng, SeedableRng, OSRng}; use {Rng, SeedableRng, OSRng};
use iter::{Iterator, range, range_step, Repeat}; use std::iter::{range_step, Repeat};
use option::{None, Some}; use std::vec::raw;
use vec::{raw, MutableVector, ImmutableVector}; use std::mem;
use mem;
static RAND_SIZE_LEN: u32 = 8; static RAND_SIZE_LEN: u32 = 8;
static RAND_SIZE: u32 = 1 << RAND_SIZE_LEN; static RAND_SIZE: u32 = 1 << RAND_SIZE_LEN;
@ -430,10 +429,9 @@ impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::{IsaacRng, Isaac64Rng};
use rand::{Rng, SeedableRng, OSRng}; use {Rng, SeedableRng, OSRng};
use prelude::*; use std::vec;
use vec;
#[test] #[test]
fn test_rng_32_rand_seeded() { fn test_rng_32_rand_seeded() {

View File

@ -48,38 +48,41 @@ randomness.
# Examples # Examples
```rust ```rust
use std::rand; use rand::Rng;
use std::rand::Rng;
let mut rng = rand::rng(); let mut rng = rand::rng();
if rng.gen() { // bool if rng.gen() { // bool
println!("int: {}, uint: {}", rng.gen::<int>(), rng.gen::<uint>()) println!("int: {}, uint: {}", rng.gen::<int>(), rng.gen::<uint>())
} }
``` ```
```rust ```rust
use std::rand;
let tuple_ptr = rand::random::<~(f64, char)>(); let tuple_ptr = rand::random::<~(f64, char)>();
println!("{:?}", tuple_ptr) println!("{:?}", tuple_ptr)
``` ```
*/ */
use cast; #[crate_id = "rand#0.10-pre"];
use cmp::Ord; #[license = "MIT/ASL2"];
use container::Container; #[crate_type = "dylib"];
use iter::{Iterator, range}; #[crate_type = "rlib"];
use kinds::marker; #[doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
use local_data; html_favicon_url = "http://www.rust-lang.org/favicon.ico",
use prelude::*; html_root_url = "http://static.rust-lang.org/doc/master")];
use str;
use vec;
pub use self::isaac::{IsaacRng, Isaac64Rng}; #[feature(macro_rules, managed_boxes)];
pub use self::os::OSRng;
use self::distributions::{Range, IndependentSample}; use std::cast;
use self::distributions::range::SampleRange; use std::kinds::marker;
use std::local_data;
use std::str;
use std::vec;
pub use isaac::{IsaacRng, Isaac64Rng};
pub use os::OSRng;
use distributions::{Range, IndependentSample};
use distributions::range::SampleRange;
pub mod distributions; pub mod distributions;
pub mod isaac; pub mod isaac;
@ -135,7 +138,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let mut v = [0u8, .. 13579]; /// let mut v = [0u8, .. 13579];
/// task_rng().fill_bytes(v); /// task_rng().fill_bytes(v);
@ -170,7 +173,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let mut rng = task_rng(); /// let mut rng = task_rng();
/// let x: uint = rng.gen(); /// let x: uint = rng.gen();
@ -187,7 +190,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let mut rng = task_rng(); /// let mut rng = task_rng();
/// let x: ~[uint] = rng.gen_vec(10); /// let x: ~[uint] = rng.gen_vec(10);
@ -210,7 +213,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let mut rng = task_rng(); /// let mut rng = task_rng();
/// let n: uint = rng.gen_range(0u, 10); /// let n: uint = rng.gen_range(0u, 10);
@ -228,7 +231,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let mut rng = task_rng(); /// let mut rng = task_rng();
/// println!("{:b}", rng.gen_weighted_bool(3)); /// println!("{:b}", rng.gen_weighted_bool(3));
@ -243,7 +246,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// println!("{}", task_rng().gen_ascii_str(10)); /// println!("{}", task_rng().gen_ascii_str(10));
/// ``` /// ```
@ -269,7 +272,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let choices = [1, 2, 4, 8, 16, 32]; /// let choices = [1, 2, 4, 8, 16, 32];
/// let mut rng = task_rng(); /// let mut rng = task_rng();
@ -289,7 +292,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// println!("{:?}", task_rng().shuffle(~[1,2,3])); /// println!("{:?}", task_rng().shuffle(~[1,2,3]));
/// ``` /// ```
@ -304,7 +307,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let mut rng = task_rng(); /// let mut rng = task_rng();
/// let mut y = [1,2,3]; /// let mut y = [1,2,3];
@ -328,7 +331,7 @@ pub trait Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{task_rng, Rng}; /// use rand::{task_rng, Rng};
/// ///
/// let mut rng = task_rng(); /// let mut rng = task_rng();
/// let sample = rng.sample(range(1, 100), 5); /// let sample = rng.sample(range(1, 100), 5);
@ -359,7 +362,7 @@ pub trait SeedableRng<Seed>: Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{Rng, SeedableRng, StdRng}; /// use rand::{Rng, SeedableRng, StdRng};
/// ///
/// let mut rng: StdRng = SeedableRng::from_seed(&[1, 2, 3, 4]); /// let mut rng: StdRng = SeedableRng::from_seed(&[1, 2, 3, 4]);
/// println!("{}", rng.gen::<f64>()); /// println!("{}", rng.gen::<f64>());
@ -373,7 +376,7 @@ pub trait SeedableRng<Seed>: Rng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{Rng, SeedableRng, StdRng}; /// use rand::{Rng, SeedableRng, StdRng};
/// ///
/// let mut rng: StdRng = SeedableRng::from_seed(&[1, 2, 3, 4]); /// let mut rng: StdRng = SeedableRng::from_seed(&[1, 2, 3, 4]);
/// println!("{}", rng.gen::<f64>()); /// println!("{}", rng.gen::<f64>());
@ -609,7 +612,7 @@ impl Rng for TaskRng {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::random; /// use rand::random;
/// ///
/// if random() { /// if random() {
/// let x = random(); /// let x = random();
@ -631,8 +634,8 @@ pub fn random<T: Rand>() -> T {
/// `[0,1)`. /// `[0,1)`.
/// ///
/// # Example /// # Example
/// ```rust,ignore /// ```rust
/// use std::rand::{random, Open01}; /// use rand::{random, Open01};
/// ///
/// let Open01(val) = random::<Open01<f32>>(); /// let Open01(val) = random::<Open01<f32>>();
/// println!("f32 from (0,1): {}", val); /// println!("f32 from (0,1): {}", val);
@ -647,8 +650,8 @@ pub struct Open01<F>(F);
/// `[0,1)`. /// `[0,1)`.
/// ///
/// # Example /// # Example
/// ```rust,ignore /// ```rust
/// use std::rand::{random, Closed01}; /// use rand::{random, Closed01};
/// ///
/// let Closed01(val) = random::<Closed01<f32>>(); /// let Closed01(val) = random::<Closed01<f32>>();
/// println!("f32 from [0,1]: {}", val); /// println!("f32 from [0,1]: {}", val);
@ -657,9 +660,8 @@ pub struct Closed01<F>(F);
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use prelude::*; use std::vec;
use vec; use super::{Rng, task_rng, random, OSRng, SeedableRng, StdRng};
use super::*;
struct ConstRng { i: u64 } struct ConstRng { i: u64 }
impl Rng for ConstRng { impl Rng for ConstRng {
@ -691,7 +693,7 @@ mod test {
#[test] #[test]
fn test_gen_range() { fn test_gen_range() {
let mut r = rng(); let mut r = task_rng();
for _ in range(0, 1000) { for _ in range(0, 1000) {
let a = r.gen_range(-3i, 42); let a = r.gen_range(-3i, 42);
assert!(a >= -3 && a < 42); assert!(a >= -3 && a < 42);
@ -711,20 +713,20 @@ mod test {
#[test] #[test]
#[should_fail] #[should_fail]
fn test_gen_range_fail_int() { fn test_gen_range_fail_int() {
let mut r = rng(); let mut r = task_rng();
r.gen_range(5i, -2); r.gen_range(5i, -2);
} }
#[test] #[test]
#[should_fail] #[should_fail]
fn test_gen_range_fail_uint() { fn test_gen_range_fail_uint() {
let mut r = rng(); let mut r = task_rng();
r.gen_range(5u, 2u); r.gen_range(5u, 2u);
} }
#[test] #[test]
fn test_gen_f64() { fn test_gen_f64() {
let mut r = rng(); let mut r = task_rng();
let a = r.gen::<f64>(); let a = r.gen::<f64>();
let b = r.gen::<f64>(); let b = r.gen::<f64>();
debug!("{:?}", (a, b)); debug!("{:?}", (a, b));
@ -732,14 +734,14 @@ mod test {
#[test] #[test]
fn test_gen_weighted_bool() { fn test_gen_weighted_bool() {
let mut r = rng(); let mut r = task_rng();
assert_eq!(r.gen_weighted_bool(0u), true); assert_eq!(r.gen_weighted_bool(0u), true);
assert_eq!(r.gen_weighted_bool(1u), true); assert_eq!(r.gen_weighted_bool(1u), true);
} }
#[test] #[test]
fn test_gen_ascii_str() { fn test_gen_ascii_str() {
let mut r = rng(); let mut r = task_rng();
debug!("{}", r.gen_ascii_str(10u)); debug!("{}", r.gen_ascii_str(10u));
debug!("{}", r.gen_ascii_str(10u)); debug!("{}", r.gen_ascii_str(10u));
debug!("{}", r.gen_ascii_str(10u)); debug!("{}", r.gen_ascii_str(10u));
@ -750,7 +752,7 @@ mod test {
#[test] #[test]
fn test_gen_vec() { fn test_gen_vec() {
let mut r = rng(); let mut r = task_rng();
assert_eq!(r.gen_vec::<u8>(0u).len(), 0u); assert_eq!(r.gen_vec::<u8>(0u).len(), 0u);
assert_eq!(r.gen_vec::<u8>(10u).len(), 10u); assert_eq!(r.gen_vec::<u8>(10u).len(), 10u);
assert_eq!(r.gen_vec::<f64>(16u).len(), 16u); assert_eq!(r.gen_vec::<f64>(16u).len(), 16u);
@ -758,13 +760,13 @@ mod test {
#[test] #[test]
fn test_choose() { fn test_choose() {
let mut r = rng(); let mut r = task_rng();
assert_eq!(r.choose([1, 1, 1]), 1); assert_eq!(r.choose([1, 1, 1]), 1);
} }
#[test] #[test]
fn test_choose_option() { fn test_choose_option() {
let mut r = rng(); let mut r = task_rng();
let v: &[int] = &[]; let v: &[int] = &[];
assert!(r.choose_option(v).is_none()); assert!(r.choose_option(v).is_none());
@ -775,7 +777,7 @@ mod test {
#[test] #[test]
fn test_shuffle() { fn test_shuffle() {
let mut r = rng(); let mut r = task_rng();
let empty: ~[int] = ~[]; let empty: ~[int] = ~[];
assert_eq!(r.shuffle(~[]), empty); assert_eq!(r.shuffle(~[]), empty);
assert_eq!(r.shuffle(~[1, 1, 1]), ~[1, 1, 1]); assert_eq!(r.shuffle(~[1, 1, 1]), ~[1, 1, 1]);
@ -806,7 +808,7 @@ mod test {
let min_val = 1; let min_val = 1;
let max_val = 100; let max_val = 100;
let mut r = rng(); let mut r = task_rng();
let vals = range(min_val, max_val).to_owned_vec(); let vals = range(min_val, max_val).to_owned_vec();
let small_sample = r.sample(vals.iter(), 5); let small_sample = r.sample(vals.iter(), 5);
let large_sample = r.sample(vals.iter(), vals.len() + 5); let large_sample = r.sample(vals.iter(), vals.len() + 5);
@ -847,9 +849,8 @@ static RAND_BENCH_N: u64 = 100;
mod bench { mod bench {
extern crate test; extern crate test;
use self::test::BenchHarness; use self::test::BenchHarness;
use prelude::*; use {XorShiftRng, StdRng, IsaacRng, Isaac64Rng, Rng, RAND_BENCH_N};
use rand::{XorShiftRng, StdRng, IsaacRng, Isaac64Rng, Rng, RAND_BENCH_N}; use std::mem::size_of;
use mem::size_of;
#[bench] #[bench]
fn rand_xorshift(bh: &mut BenchHarness) { fn rand_xorshift(bh: &mut BenchHarness) {

View File

@ -11,18 +11,17 @@
//! Interfaces to the operating system provided random number //! Interfaces to the operating system provided random number
//! generators. //! generators.
use rand::Rng; use Rng;
use ops::Drop;
#[cfg(unix)] #[cfg(unix)]
use rand::reader::ReaderRng; use reader::ReaderRng;
#[cfg(unix)] #[cfg(unix)]
use io::File; use std::io::File;
#[cfg(windows)] #[cfg(windows)]
use cast; use std::cast;
#[cfg(windows)] #[cfg(windows)]
use libc::{c_long, DWORD, BYTE}; use std::libc::{c_long, DWORD, BYTE};
#[cfg(windows)] #[cfg(windows)]
type HCRYPTPROV = c_long; type HCRYPTPROV = c_long;
// the extern functions imported from the runtime on Windows are // the extern functions imported from the runtime on Windows are
@ -60,7 +59,6 @@ impl OSRng {
/// Create a new `OSRng`. /// Create a new `OSRng`.
#[cfg(unix)] #[cfg(unix)]
pub fn new() -> OSRng { pub fn new() -> OSRng {
use path::Path;
let reader = File::open(&Path::new("/dev/urandom")); let reader = File::open(&Path::new("/dev/urandom"));
let reader = reader.ok().expect("Error opening /dev/urandom"); let reader = reader.ok().expect("Error opening /dev/urandom");
let reader_rng = ReaderRng::new(reader); let reader_rng = ReaderRng::new(reader);
@ -106,9 +104,6 @@ impl Rng for OSRng {
unsafe { cast::transmute(v) } unsafe { cast::transmute(v) }
} }
fn fill_bytes(&mut self, v: &mut [u8]) { fn fill_bytes(&mut self, v: &mut [u8]) {
use container::Container;
use vec::MutableVector;
extern { extern {
fn rust_win32_rand_gen(hProv: HCRYPTPROV, dwLen: DWORD, fn rust_win32_rand_gen(hProv: HCRYPTPROV, dwLen: DWORD,
pbBuffer: *mut BYTE); pbBuffer: *mut BYTE);
@ -136,10 +131,9 @@ impl Drop for OSRng {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use prelude::*; use super::OSRng;
use super::*; use Rng;
use rand::Rng; use std::task;
use task;
#[test] #[test]
fn test_os_rng() { fn test_os_rng() {

View File

@ -10,11 +10,11 @@
//! The implementations of `Rand` for the built-in types. //! The implementations of `Rand` for the built-in types.
use char; use std::char;
use int; use std::int;
use option::{Option, Some, None}; use std::uint;
use rand::{Rand,Rng};
use uint; use {Rand,Rng};
impl Rand for int { impl Rand for int {
#[inline] #[inline]
@ -97,7 +97,7 @@ impl Rand for u64 {
macro_rules! float_impls { macro_rules! float_impls {
($mod_name:ident, $ty:ty, $mantissa_bits:expr, $method_name:ident, $ignored_bits:expr) => { ($mod_name:ident, $ty:ty, $mantissa_bits:expr, $method_name:ident, $ignored_bits:expr) => {
mod $mod_name { mod $mod_name {
use rand::{Rand, Rng, Open01, Closed01}; use {Rand, Rng, Open01, Closed01};
static SCALE: $ty = (1u64 << $mantissa_bits) as $ty; static SCALE: $ty = (1u64 << $mantissa_bits) as $ty;
@ -226,8 +226,7 @@ impl<T: Rand + 'static> Rand for @T {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use prelude::*; use {Rng, task_rng, Open01, Closed01};
use rand::{Rng, task_rng, Open01, Closed01};
struct ConstantRng(u64); struct ConstantRng(u64);
impl Rng for ConstantRng { impl Rng for ConstantRng {

View File

@ -10,11 +10,7 @@
//! A wrapper around any Reader to treat it as an RNG. //! A wrapper around any Reader to treat it as an RNG.
use container::Container; use Rng;
use result::{Ok, Err};
use io::Reader;
use rand::Rng;
/// An RNG that reads random bytes straight from a `Reader`. This will /// An RNG that reads random bytes straight from a `Reader`. This will
/// work best with an infinite reader, but this is not required. /// work best with an infinite reader, but this is not required.
@ -24,7 +20,7 @@ use rand::Rng;
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand::{reader, Rng}; /// use rand::{reader, Rng};
/// use std::io::MemReader; /// use std::io::MemReader;
/// ///
/// let mut rng = reader::ReaderRng::new(MemReader::new(~[1,2,3,4,5,6,7,8])); /// let mut rng = reader::ReaderRng::new(MemReader::new(~[1,2,3,4,5,6,7,8]));
@ -75,11 +71,10 @@ impl<R: Reader> Rng for ReaderRng<R> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::ReaderRng;
use io::MemReader; use std::io::MemReader;
use cast; use std::cast;
use rand::*; use Rng;
use prelude::*;
#[test] #[test]
fn test_reader_rng_u64() { fn test_reader_rng_u64() {

View File

@ -11,9 +11,8 @@
//! A wrapper around another RNG that reseeds it after it //! A wrapper around another RNG that reseeds it after it
//! generates a certain number of random bytes. //! generates a certain number of random bytes.
use container::Container; use std::default::Default;
use default::Default; use {Rng, SeedableRng};
use rand::{Rng, SeedableRng};
/// How many bytes of entropy the underling RNG is allowed to generate /// How many bytes of entropy the underling RNG is allowed to generate
/// before it is reseeded. /// before it is reseeded.
@ -101,9 +100,8 @@ impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R>>
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::rand; /// use rand::{Rng, SeedableRng};
/// use std::rand::{Rng, SeedableRng}; /// use rand::reseeding::{Reseeder, ReseedingRng};
/// use std::rand::reseeding::{Reseeder, ReseedingRng};
/// ///
/// struct TickTockReseeder { tick: bool } /// struct TickTockReseeder { tick: bool }
/// impl Reseeder<rand::StdRng> for TickTockReseeder { /// impl Reseeder<rand::StdRng> for TickTockReseeder {
@ -142,10 +140,9 @@ impl Default for ReseedWithDefault {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use prelude::*; use super::{ReseedingRng, ReseedWithDefault};
use super::*; use std::default::Default;
use default::Default; use {SeedableRng, Rng};
use rand::{SeedableRng, Rng};
struct Counter { struct Counter {
i: u32 i: u32
@ -205,7 +202,7 @@ mod test {
static fill_bytes_v_len: uint = 13579; static fill_bytes_v_len: uint = 13579;
#[test] #[test]
fn test_rng_fill_bytes() { fn test_rng_fill_bytes() {
use rand::task_rng; use task_rng;
let mut v = ~[0u8, .. fill_bytes_v_len]; let mut v = ~[0u8, .. fill_bytes_v_len];
task_rng().fill_bytes(v); task_rng().fill_bytes(v);

View File

@ -73,7 +73,8 @@
#[cfg(test)] extern crate native; #[cfg(test)] extern crate native;
#[cfg(test)] extern crate green; #[cfg(test)] extern crate green;
// Make extra accessible for benchmarking // Make extra and rand accessible for benchmarking/testcases
#[cfg(test)] extern crate rand;
#[cfg(test)] extern crate extra = "extra"; #[cfg(test)] extern crate extra = "extra";
// Make std testable by not duplicating lang items. See #2912 // Make std testable by not duplicating lang items. See #2912
@ -173,7 +174,6 @@ pub mod c_str;
pub mod os; pub mod os;
pub mod io; pub mod io;
pub mod path; pub mod path;
pub mod rand;
pub mod cast; pub mod cast;
pub mod fmt; pub mod fmt;
pub mod cleanup; pub mod cleanup;

View File

@ -26,7 +26,7 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt,
let trait_def = TraitDef { let trait_def = TraitDef {
span: span, span: span,
attributes: Vec::new(), attributes: Vec::new(),
path: Path::new(vec!("std", "rand", "Rand")), path: Path::new(vec!("rand", "Rand")),
additional_bounds: Vec::new(), additional_bounds: Vec::new(),
generics: LifetimeBounds::empty(), generics: LifetimeBounds::empty(),
methods: vec!( methods: vec!(
@ -35,7 +35,7 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt,
generics: LifetimeBounds { generics: LifetimeBounds {
lifetimes: Vec::new(), lifetimes: Vec::new(),
bounds: vec!(("R", bounds: vec!(("R",
vec!( Path::new(vec!("std", "rand", "Rng")) ))) vec!( Path::new(vec!("rand", "Rng")) )))
}, },
explicit_self: None, explicit_self: None,
args: vec!( args: vec!(
@ -58,7 +58,6 @@ fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
_ => cx.bug("Incorrect number of arguments to `rand` in `deriving(Rand)`") _ => cx.bug("Incorrect number of arguments to `rand` in `deriving(Rand)`")
}; };
let rand_ident = vec!( let rand_ident = vec!(
cx.ident_of("std"),
cx.ident_of("rand"), cx.ident_of("rand"),
cx.ident_of("Rand"), cx.ident_of("Rand"),
cx.ident_of("rand") cx.ident_of("rand")
@ -89,7 +88,7 @@ fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
Vec::new()); Vec::new());
let rand_name = cx.expr_path(rand_name); let rand_name = cx.expr_path(rand_name);
// ::std::rand::Rand::rand(rng) // ::rand::Rand::rand(rng)
let rv_call = cx.expr_call(trait_span, let rv_call = cx.expr_call(trait_span,
rand_name, rand_name,
vec!( *rng.get(0) )); vec!( *rng.get(0) ));

View File

@ -410,7 +410,7 @@ rust_win32_rand_acquire(HCRYPTPROV* phProv) {
win32_require win32_require
(_T("CryptAcquireContext"), (_T("CryptAcquireContext"),
// changes to the parameters here should be reflected in the docs of // changes to the parameters here should be reflected in the docs of
// std::rand::os::OSRng // rand::os::OSRng
CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL, CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT|CRYPT_SILENT)); CRYPT_VERIFYCONTEXT|CRYPT_SILENT));

View File

@ -10,9 +10,11 @@
// ensure that the TaskRng isn't/doesn't become accidentally sendable. // ensure that the TaskRng isn't/doesn't become accidentally sendable.
extern crate rand;
fn test_send<S: Send>() {} fn test_send<S: Send>() {}
pub fn main() { pub fn main() {
test_send::<::std::rand::TaskRng>(); test_send::<::rand::TaskRng>();
//~^ ERROR: incompatible type `std::rand::TaskRng`, which does not fulfill `Send` //~^ ERROR: incompatible type `rand::TaskRng`, which does not fulfill `Send`
} }