2013-09-29 16:49:11 +10:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! A wrapper around another RNG that reseeds it after it
|
|
|
|
//! generates a certain number of random bytes.
|
|
|
|
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2014-03-02 11:23:04 +11:00
|
|
|
use {Rng, SeedableRng};
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
use core::default::Default;
|
2013-09-29 16:49:11 +10:00
|
|
|
|
|
|
|
/// How many bytes of entropy the underling RNG is allowed to generate
|
|
|
|
/// before it is reseeded.
|
|
|
|
static DEFAULT_GENERATION_THRESHOLD: uint = 32 * 1024;
|
|
|
|
|
|
|
|
/// A wrapper around any RNG which reseeds the underlying RNG after it
|
|
|
|
/// has generated a certain number of random bytes.
|
|
|
|
pub struct ReseedingRng<R, Rsdr> {
|
2014-03-27 15:10:38 -07:00
|
|
|
rng: R,
|
|
|
|
generation_threshold: uint,
|
|
|
|
bytes_generated: uint,
|
2013-09-29 16:49:11 +10:00
|
|
|
/// Controls the behaviour when reseeding the RNG.
|
2014-03-27 15:10:38 -07:00
|
|
|
pub reseeder: Rsdr,
|
2013-09-29 16:49:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Rng, Rsdr: Reseeder<R>> ReseedingRng<R, Rsdr> {
|
|
|
|
/// Create a new `ReseedingRng` with the given parameters.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `rng`: the random number generator to use.
|
|
|
|
/// * `generation_threshold`: the number of bytes of entropy at which to reseed the RNG.
|
|
|
|
/// * `reseeder`: the reseeding object to use.
|
|
|
|
pub fn new(rng: R, generation_threshold: uint, reseeder: Rsdr) -> ReseedingRng<R,Rsdr> {
|
|
|
|
ReseedingRng {
|
|
|
|
rng: rng,
|
|
|
|
generation_threshold: generation_threshold,
|
|
|
|
bytes_generated: 0,
|
|
|
|
reseeder: reseeder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reseed the internal RNG if the number of bytes that have been
|
|
|
|
/// generated exceed the threshold.
|
|
|
|
pub fn reseed_if_necessary(&mut self) {
|
|
|
|
if self.bytes_generated >= self.generation_threshold {
|
|
|
|
self.reseeder.reseed(&mut self.rng);
|
|
|
|
self.bytes_generated = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<R: Rng, Rsdr: Reseeder<R>> Rng for ReseedingRng<R, Rsdr> {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
self.reseed_if_necessary();
|
|
|
|
self.bytes_generated += 4;
|
|
|
|
self.rng.next_u32()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
self.reseed_if_necessary();
|
|
|
|
self.bytes_generated += 8;
|
|
|
|
self.rng.next_u64()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
|
|
|
self.reseed_if_necessary();
|
|
|
|
self.bytes_generated += dest.len();
|
2013-10-31 19:26:33 -05:00
|
|
|
self.rng.fill_bytes(dest)
|
2013-09-29 16:49:11 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
impl<S, R: SeedableRng<S>, Rsdr: Reseeder<R> + Default>
|
2013-10-09 02:22:37 +11:00
|
|
|
SeedableRng<(Rsdr, S)> for ReseedingRng<R, Rsdr> {
|
|
|
|
fn reseed(&mut self, (rsdr, seed): (Rsdr, S)) {
|
2013-10-01 01:32:12 +10:00
|
|
|
self.rng.reseed(seed);
|
2013-10-09 02:22:37 +11:00
|
|
|
self.reseeder = rsdr;
|
2013-10-01 01:32:12 +10:00
|
|
|
self.bytes_generated = 0;
|
|
|
|
}
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
|
2013-10-09 02:22:37 +11:00
|
|
|
/// Create a new `ReseedingRng` from the given reseeder and
|
|
|
|
/// seed. This uses a default value for `generation_threshold`.
|
|
|
|
fn from_seed((rsdr, seed): (Rsdr, S)) -> ReseedingRng<R, Rsdr> {
|
2013-10-01 01:32:12 +10:00
|
|
|
ReseedingRng {
|
|
|
|
rng: SeedableRng::from_seed(seed),
|
|
|
|
generation_threshold: DEFAULT_GENERATION_THRESHOLD,
|
|
|
|
bytes_generated: 0,
|
2013-10-09 02:22:37 +11:00
|
|
|
reseeder: rsdr
|
2013-10-01 01:32:12 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 16:49:11 +10:00
|
|
|
/// Something that can be used to reseed an RNG via `ReseedingRng`.
|
2013-10-02 03:17:57 +10:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
/// use std::rand::{Rng, SeedableRng, StdRng};
|
|
|
|
/// use std::rand::reseeding::{Reseeder, ReseedingRng};
|
2013-10-02 03:17:57 +10:00
|
|
|
///
|
|
|
|
/// struct TickTockReseeder { tick: bool }
|
2014-03-25 16:13:11 +11:00
|
|
|
/// impl Reseeder<StdRng> for TickTockReseeder {
|
|
|
|
/// fn reseed(&mut self, rng: &mut StdRng) {
|
2013-10-02 03:17:57 +10:00
|
|
|
/// let val = if self.tick {0} else {1};
|
|
|
|
/// rng.reseed(&[val]);
|
|
|
|
/// self.tick = !self.tick;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// fn main() {
|
|
|
|
/// let rsdr = TickTockReseeder { tick: true };
|
2014-03-25 16:13:11 +11:00
|
|
|
///
|
|
|
|
/// let inner = StdRng::new().unwrap();
|
|
|
|
/// let mut rng = ReseedingRng::new(inner, 10, rsdr);
|
2013-10-02 03:17:57 +10:00
|
|
|
///
|
|
|
|
/// // this will repeat, because it gets reseeded very regularly.
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
/// let s: String = rng.gen_ascii_chars().take(100).collect();
|
|
|
|
/// println!("{}", s);
|
2013-10-02 03:17:57 +10:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// ```
|
2013-09-29 16:49:11 +10:00
|
|
|
pub trait Reseeder<R> {
|
|
|
|
/// Reseed the given RNG.
|
|
|
|
fn reseed(&mut self, rng: &mut R);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reseed an RNG using a `Default` instance. This reseeds by
|
|
|
|
/// replacing the RNG with the result of a `Default::default` call.
|
|
|
|
pub struct ReseedWithDefault;
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
impl Copy for ReseedWithDefault {}
|
|
|
|
|
2013-09-29 16:49:11 +10:00
|
|
|
impl<R: Rng + Default> Reseeder<R> for ReseedWithDefault {
|
|
|
|
fn reseed(&mut self, rng: &mut R) {
|
|
|
|
*rng = Default::default();
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 02:23:22 +10:00
|
|
|
impl Default for ReseedWithDefault {
|
|
|
|
fn default() -> ReseedWithDefault { ReseedWithDefault }
|
|
|
|
}
|
2013-09-29 16:49:11 +10:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
use std::prelude::*;
|
|
|
|
|
|
|
|
use core::iter::order;
|
2014-03-02 11:23:04 +11:00
|
|
|
use super::{ReseedingRng, ReseedWithDefault};
|
|
|
|
use std::default::Default;
|
|
|
|
use {SeedableRng, Rng};
|
2013-09-29 16:49:11 +10:00
|
|
|
|
|
|
|
struct Counter {
|
|
|
|
i: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng for Counter {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
self.i += 1;
|
|
|
|
// very random
|
|
|
|
self.i - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Default for Counter {
|
|
|
|
fn default() -> Counter {
|
|
|
|
Counter { i: 0 }
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 02:23:22 +10:00
|
|
|
impl SeedableRng<u32> for Counter {
|
|
|
|
fn reseed(&mut self, seed: u32) {
|
|
|
|
self.i = seed;
|
|
|
|
}
|
|
|
|
fn from_seed(seed: u32) -> Counter {
|
|
|
|
Counter { i: seed }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
type MyRng = ReseedingRng<Counter, ReseedWithDefault>;
|
2013-09-29 16:49:11 +10:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reseeding() {
|
2013-09-30 01:29:28 +10:00
|
|
|
let mut rs = ReseedingRng::new(Counter {i:0}, 400, ReseedWithDefault);
|
2013-09-29 16:49:11 +10:00
|
|
|
|
|
|
|
let mut i = 0;
|
2014-04-21 17:58:52 -04:00
|
|
|
for _ in range(0u, 1000) {
|
2013-09-29 16:49:11 +10:00
|
|
|
assert_eq!(rs.next_u32(), i % 100);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 02:23:22 +10:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rng_seeded() {
|
2013-10-09 02:22:37 +11:00
|
|
|
let mut ra: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
|
|
|
|
let mut rb: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
assert!(order::equals(ra.gen_ascii_chars().take(100),
|
|
|
|
rb.gen_ascii_chars().take(100)));
|
2013-10-02 02:23:22 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rng_reseed() {
|
2013-10-09 02:22:37 +11:00
|
|
|
let mut r: MyRng = SeedableRng::from_seed((ReseedWithDefault, 3));
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
let string1: String = r.gen_ascii_chars().take(100).collect();
|
2013-10-02 02:23:22 +10:00
|
|
|
|
2013-10-09 02:22:37 +11:00
|
|
|
r.reseed((ReseedWithDefault, 3));
|
2013-10-02 02:23:22 +10:00
|
|
|
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
let string2: String = r.gen_ascii_chars().take(100).collect();
|
2013-10-02 02:23:22 +10:00
|
|
|
assert_eq!(string1, string2);
|
|
|
|
}
|
2013-10-31 19:26:33 -05:00
|
|
|
|
2014-09-13 13:55:37 +12:00
|
|
|
static FILL_BYTES_V_LEN: uint = 13579;
|
2013-10-31 19:26:33 -05:00
|
|
|
#[test]
|
|
|
|
fn test_rng_fill_bytes() {
|
2014-09-13 13:55:37 +12:00
|
|
|
let mut v = Vec::from_elem(FILL_BYTES_V_LEN, 0u8);
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 01:39:37 -07:00
|
|
|
::test::rng().fill_bytes(v.as_mut_slice());
|
2013-10-31 19:26:33 -05:00
|
|
|
|
|
|
|
// Sanity test: if we've gotten here, `fill_bytes` has not infinitely
|
|
|
|
// recursed.
|
2014-09-13 13:55:37 +12:00
|
|
|
assert_eq!(v.len(), FILL_BYTES_V_LEN);
|
2013-10-31 19:26:33 -05:00
|
|
|
|
|
|
|
// To test that `fill_bytes` actually did something, check that the
|
|
|
|
// average of `v` is not 0.
|
|
|
|
let mut sum = 0.0;
|
|
|
|
for &x in v.iter() {
|
|
|
|
sum += x as f64;
|
|
|
|
}
|
|
|
|
assert!(sum / v.len() as f64 != 0.0);
|
|
|
|
}
|
2013-09-29 16:49:11 +10:00
|
|
|
}
|