2013-09-21 06:32:57 -05: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.
|
|
|
|
|
|
|
|
//! The ISAAC random number generator.
|
|
|
|
|
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 03:39:37 -05:00
|
|
|
use core::prelude::*;
|
2014-11-20 12:11:15 -06:00
|
|
|
use core::slice;
|
2014-11-06 11:32:37 -06:00
|
|
|
use core::iter::{range_step, repeat};
|
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 03:39:37 -05:00
|
|
|
|
|
|
|
use {Rng, SeedableRng, Rand};
|
2013-09-21 06:32:57 -05:00
|
|
|
|
2014-10-06 18:18:57 -05:00
|
|
|
const RAND_SIZE_LEN: u32 = 8;
|
|
|
|
const RAND_SIZE: u32 = 1 << (RAND_SIZE_LEN as uint);
|
|
|
|
const RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);
|
2013-09-21 06:32:57 -05:00
|
|
|
|
2013-10-11 01:25:40 -05:00
|
|
|
/// A random number generator that uses the ISAAC algorithm[1].
|
2013-09-21 06:32:57 -05:00
|
|
|
///
|
2013-10-11 01:25:40 -05:00
|
|
|
/// The ISAAC algorithm is generally accepted as suitable for
|
|
|
|
/// cryptographic purposes, but this implementation has not be
|
2014-05-29 23:37:31 -05:00
|
|
|
/// verified as such. Prefer a generator like `OsRng` that defers to
|
2013-10-11 01:25:40 -05:00
|
|
|
/// the operating system for cases that need high security.
|
|
|
|
///
|
|
|
|
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
|
|
|
|
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
|
2013-09-21 06:32:57 -05:00
|
|
|
pub struct IsaacRng {
|
2014-03-27 17:10:38 -05:00
|
|
|
cnt: u32,
|
2014-04-21 16:58:52 -05:00
|
|
|
rsl: [u32, ..RAND_SIZE_UINT],
|
|
|
|
mem: [u32, ..RAND_SIZE_UINT],
|
2014-03-27 17:10:38 -05:00
|
|
|
a: u32,
|
|
|
|
b: u32,
|
|
|
|
c: u32
|
2013-09-21 06:32:57 -05:00
|
|
|
}
|
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 19:01:33 -06:00
|
|
|
|
|
|
|
impl Copy for IsaacRng {}
|
|
|
|
|
2013-10-08 08:21:26 -05:00
|
|
|
static EMPTY: IsaacRng = IsaacRng {
|
|
|
|
cnt: 0,
|
2014-04-21 16:58:52 -05:00
|
|
|
rsl: [0, ..RAND_SIZE_UINT],
|
|
|
|
mem: [0, ..RAND_SIZE_UINT],
|
2013-10-08 08:21:26 -05:00
|
|
|
a: 0, b: 0, c: 0
|
|
|
|
};
|
2013-09-21 06:32:57 -05:00
|
|
|
|
|
|
|
impl IsaacRng {
|
2014-08-04 07:19:02 -05:00
|
|
|
|
2013-09-21 06:32:57 -05:00
|
|
|
/// Create an ISAAC random number generator using the default
|
|
|
|
/// fixed seed.
|
|
|
|
pub fn new_unseeded() -> IsaacRng {
|
2013-10-08 08:21:26 -05:00
|
|
|
let mut rng = EMPTY;
|
2013-09-21 06:32:57 -05:00
|
|
|
rng.init(false);
|
|
|
|
rng
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialises `self`. If `use_rsl` is true, then use the current value
|
|
|
|
/// of `rsl` as a seed, otherwise construct one algorithmically (not
|
|
|
|
/// randomly).
|
|
|
|
fn init(&mut self, use_rsl: bool) {
|
|
|
|
let mut a = 0x9e3779b9;
|
|
|
|
let mut b = a;
|
|
|
|
let mut c = a;
|
|
|
|
let mut d = a;
|
|
|
|
let mut e = a;
|
|
|
|
let mut f = a;
|
|
|
|
let mut g = a;
|
|
|
|
let mut h = a;
|
|
|
|
|
|
|
|
macro_rules! mix(
|
|
|
|
() => {{
|
|
|
|
a^=b<<11; d+=a; b+=c;
|
|
|
|
b^=c>>2; e+=b; c+=d;
|
|
|
|
c^=d<<8; f+=c; d+=e;
|
|
|
|
d^=e>>16; g+=d; e+=f;
|
|
|
|
e^=f<<10; h+=e; f+=g;
|
|
|
|
f^=g>>4; a+=f; g+=h;
|
|
|
|
g^=h<<8; b+=g; h+=a;
|
|
|
|
h^=a>>9; c+=h; a+=b;
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 4) {
|
|
|
|
mix!();
|
|
|
|
}
|
2013-09-21 06:32:57 -05:00
|
|
|
|
|
|
|
if use_rsl {
|
|
|
|
macro_rules! memloop (
|
|
|
|
($arr:expr) => {{
|
2014-04-01 22:39:26 -05:00
|
|
|
for i in range_step(0, RAND_SIZE as uint, 8) {
|
2013-09-21 06:32:57 -05:00
|
|
|
a+=$arr[i ]; b+=$arr[i+1];
|
|
|
|
c+=$arr[i+2]; d+=$arr[i+3];
|
|
|
|
e+=$arr[i+4]; f+=$arr[i+5];
|
|
|
|
g+=$arr[i+6]; h+=$arr[i+7];
|
|
|
|
mix!();
|
|
|
|
self.mem[i ]=a; self.mem[i+1]=b;
|
|
|
|
self.mem[i+2]=c; self.mem[i+3]=d;
|
|
|
|
self.mem[i+4]=e; self.mem[i+5]=f;
|
|
|
|
self.mem[i+6]=g; self.mem[i+7]=h;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
|
|
|
|
memloop!(self.rsl);
|
|
|
|
memloop!(self.mem);
|
|
|
|
} else {
|
2014-04-01 22:39:26 -05:00
|
|
|
for i in range_step(0, RAND_SIZE as uint, 8) {
|
2013-09-21 06:32:57 -05:00
|
|
|
mix!();
|
|
|
|
self.mem[i ]=a; self.mem[i+1]=b;
|
|
|
|
self.mem[i+2]=c; self.mem[i+3]=d;
|
|
|
|
self.mem[i+4]=e; self.mem[i+5]=f;
|
|
|
|
self.mem[i+6]=g; self.mem[i+7]=h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.isaac();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Refills the output buffer (`self.rsl`)
|
|
|
|
#[inline]
|
2014-10-27 17:37:07 -05:00
|
|
|
#[allow(unsigned_negation)]
|
2013-09-21 06:32:57 -05:00
|
|
|
fn isaac(&mut self) {
|
|
|
|
self.c += 1;
|
|
|
|
// abbreviations
|
|
|
|
let mut a = self.a;
|
|
|
|
let mut b = self.b + self.c;
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
static MIDPOINT: uint = (RAND_SIZE / 2) as uint;
|
2013-09-21 06:32:57 -05:00
|
|
|
|
|
|
|
macro_rules! ind (($x:expr) => {
|
2014-04-21 16:58:52 -05:00
|
|
|
self.mem[(($x >> 2) as uint & ((RAND_SIZE - 1) as uint))]
|
2013-09-21 06:32:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let r = [(0, MIDPOINT), (MIDPOINT, 0)];
|
|
|
|
for &(mr_offset, m2_offset) in r.iter() {
|
2014-06-24 20:57:00 -05:00
|
|
|
|
|
|
|
macro_rules! rngstepp(
|
|
|
|
($j:expr, $shift:expr) => {{
|
|
|
|
let base = $j;
|
|
|
|
let mix = a << $shift as uint;
|
|
|
|
|
|
|
|
let x = self.mem[base + mr_offset];
|
|
|
|
a = (a ^ mix) + self.mem[base + m2_offset];
|
|
|
|
let y = ind!(x) + a + b;
|
|
|
|
self.mem[base + mr_offset] = y;
|
|
|
|
|
|
|
|
b = ind!(y >> RAND_SIZE_LEN as uint) + x;
|
|
|
|
self.rsl[base + mr_offset] = b;
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
macro_rules! rngstepn(
|
|
|
|
($j:expr, $shift:expr) => {{
|
|
|
|
let base = $j;
|
|
|
|
let mix = a >> $shift as uint;
|
|
|
|
|
|
|
|
let x = self.mem[base + mr_offset];
|
|
|
|
a = (a ^ mix) + self.mem[base + m2_offset];
|
|
|
|
let y = ind!(x) + a + b;
|
|
|
|
self.mem[base + mr_offset] = y;
|
|
|
|
|
|
|
|
b = ind!(y >> RAND_SIZE_LEN as uint) + x;
|
|
|
|
self.rsl[base + mr_offset] = b;
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
|
2013-09-21 06:32:57 -05:00
|
|
|
for i in range_step(0u, MIDPOINT, 4) {
|
2014-04-21 16:58:52 -05:00
|
|
|
rngstepp!(i + 0, 13);
|
|
|
|
rngstepn!(i + 1, 6);
|
|
|
|
rngstepp!(i + 2, 2);
|
|
|
|
rngstepn!(i + 3, 16);
|
2013-09-21 06:32:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.a = a;
|
|
|
|
self.b = b;
|
|
|
|
self.cnt = RAND_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng for IsaacRng {
|
|
|
|
#[inline]
|
2013-09-21 07:06:50 -05:00
|
|
|
fn next_u32(&mut self) -> u32 {
|
2013-09-21 06:32:57 -05:00
|
|
|
if self.cnt == 0 {
|
|
|
|
// make some more numbers
|
|
|
|
self.isaac();
|
|
|
|
}
|
|
|
|
self.cnt -= 1;
|
2014-09-08 04:33:37 -05:00
|
|
|
|
|
|
|
// self.cnt is at most RAND_SIZE, but that is before the
|
|
|
|
// subtraction above. We want to index without bounds
|
|
|
|
// checking, but this could lead to incorrect code if someone
|
|
|
|
// misrefactors, so we check, sometimes.
|
|
|
|
//
|
|
|
|
// (Changes here should be reflected in Isaac64Rng.next_u64.)
|
|
|
|
debug_assert!(self.cnt < RAND_SIZE);
|
|
|
|
|
|
|
|
// (the % is cheaply telling the optimiser that we're always
|
|
|
|
// in bounds, without unsafe. NB. this is a power of two, so
|
|
|
|
// it optimises to a bitwise mask).
|
|
|
|
self.rsl[(self.cnt % RAND_SIZE) as uint]
|
2013-09-21 06:32:57 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-21 07:06:50 -05:00
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> SeedableRng<&'a [u32]> for IsaacRng {
|
|
|
|
fn reseed(&mut self, seed: &'a [u32]) {
|
2013-09-29 10:29:28 -05:00
|
|
|
// make the seed into [seed[0], seed[1], ..., seed[seed.len()
|
|
|
|
// - 1], 0, 0, ...], to fill rng.rsl.
|
2014-11-06 11:32:37 -06:00
|
|
|
let seed_iter = seed.iter().map(|&x| x).chain(repeat(0u32));
|
2013-09-29 10:29:28 -05:00
|
|
|
|
2014-09-14 22:27:36 -05:00
|
|
|
for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) {
|
2013-09-29 10:29:28 -05:00
|
|
|
*rsl_elem = seed_elem;
|
|
|
|
}
|
|
|
|
self.cnt = 0;
|
|
|
|
self.a = 0;
|
|
|
|
self.b = 0;
|
|
|
|
self.c = 0;
|
|
|
|
|
|
|
|
self.init(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an ISAAC random number generator with a seed. This can
|
|
|
|
/// be any length, although the maximum number of elements used is
|
|
|
|
/// 256 and any more will be silently ignored. A generator
|
|
|
|
/// constructed with a given seed will generate the same sequence
|
|
|
|
/// of values as all other generators constructed with that seed.
|
2013-12-10 01:16:18 -06:00
|
|
|
fn from_seed(seed: &'a [u32]) -> IsaacRng {
|
2013-10-08 08:21:26 -05:00
|
|
|
let mut rng = EMPTY;
|
2013-09-29 10:29:28 -05:00
|
|
|
rng.reseed(seed);
|
|
|
|
rng
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 03:39:37 -05:00
|
|
|
impl Rand for IsaacRng {
|
|
|
|
fn rand<R: Rng>(other: &mut R) -> IsaacRng {
|
|
|
|
let mut ret = EMPTY;
|
|
|
|
unsafe {
|
2014-11-20 12:11:15 -06:00
|
|
|
let ptr = ret.rsl.as_mut_ptr() as *mut u8;
|
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 03:39:37 -05:00
|
|
|
|
2014-11-20 12:11:15 -06:00
|
|
|
let slice = slice::from_raw_mut_buf(&ptr, (RAND_SIZE * 4) as uint);
|
|
|
|
other.fill_bytes(slice);
|
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 03:39:37 -05:00
|
|
|
}
|
|
|
|
ret.cnt = 0;
|
|
|
|
ret.a = 0;
|
|
|
|
ret.b = 0;
|
|
|
|
ret.c = 0;
|
|
|
|
|
|
|
|
ret.init(true);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2013-09-29 10:29:28 -05:00
|
|
|
|
2014-10-06 18:18:57 -05:00
|
|
|
const RAND_SIZE_64_LEN: uint = 8;
|
|
|
|
const RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
|
2013-09-21 07:06:50 -05:00
|
|
|
|
2013-10-11 01:25:40 -05:00
|
|
|
/// A random number generator that uses ISAAC-64[1], the 64-bit
|
|
|
|
/// variant of the ISAAC algorithm.
|
|
|
|
///
|
|
|
|
/// The ISAAC algorithm is generally accepted as suitable for
|
|
|
|
/// cryptographic purposes, but this implementation has not be
|
2014-05-29 23:37:31 -05:00
|
|
|
/// verified as such. Prefer a generator like `OsRng` that defers to
|
2013-10-11 01:25:40 -05:00
|
|
|
/// the operating system for cases that need high security.
|
2013-09-21 07:06:50 -05:00
|
|
|
///
|
2013-10-11 01:25:40 -05:00
|
|
|
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
|
|
|
|
/// generator*](http://www.burtleburtle.net/bob/rand/isaacafa.html)
|
2013-09-21 07:06:50 -05:00
|
|
|
pub struct Isaac64Rng {
|
2014-03-27 17:10:38 -05:00
|
|
|
cnt: uint,
|
|
|
|
rsl: [u64, .. RAND_SIZE_64],
|
|
|
|
mem: [u64, .. RAND_SIZE_64],
|
|
|
|
a: u64,
|
|
|
|
b: u64,
|
|
|
|
c: u64,
|
2013-09-21 07:06:50 -05:00
|
|
|
}
|
|
|
|
|
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 19:01:33 -06:00
|
|
|
impl Copy for Isaac64Rng {}
|
|
|
|
|
2013-10-08 08:21:26 -05:00
|
|
|
static EMPTY_64: Isaac64Rng = Isaac64Rng {
|
|
|
|
cnt: 0,
|
|
|
|
rsl: [0, .. RAND_SIZE_64],
|
|
|
|
mem: [0, .. RAND_SIZE_64],
|
|
|
|
a: 0, b: 0, c: 0,
|
|
|
|
};
|
|
|
|
|
2013-09-21 07:06:50 -05:00
|
|
|
impl Isaac64Rng {
|
|
|
|
/// Create a 64-bit ISAAC random number generator using the
|
|
|
|
/// default fixed seed.
|
|
|
|
pub fn new_unseeded() -> Isaac64Rng {
|
2013-10-08 08:21:26 -05:00
|
|
|
let mut rng = EMPTY_64;
|
2013-09-21 07:06:50 -05:00
|
|
|
rng.init(false);
|
|
|
|
rng
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialises `self`. If `use_rsl` is true, then use the current value
|
|
|
|
/// of `rsl` as a seed, otherwise construct one algorithmically (not
|
|
|
|
/// randomly).
|
|
|
|
fn init(&mut self, use_rsl: bool) {
|
|
|
|
macro_rules! init (
|
|
|
|
($var:ident) => (
|
|
|
|
let mut $var = 0x9e3779b97f4a7c13;
|
|
|
|
)
|
|
|
|
);
|
|
|
|
init!(a); init!(b); init!(c); init!(d);
|
|
|
|
init!(e); init!(f); init!(g); init!(h);
|
|
|
|
|
|
|
|
macro_rules! mix(
|
|
|
|
() => {{
|
|
|
|
a-=e; f^=h>>9; h+=a;
|
|
|
|
b-=f; g^=a<<9; a+=b;
|
|
|
|
c-=g; h^=b>>23; b+=c;
|
|
|
|
d-=h; a^=c<<15; c+=d;
|
|
|
|
e-=a; b^=d>>14; d+=e;
|
|
|
|
f-=b; c^=e<<20; e+=f;
|
|
|
|
g-=c; d^=f>>17; f+=g;
|
|
|
|
h-=d; e^=g<<14; g+=h;
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 4) {
|
|
|
|
mix!();
|
|
|
|
}
|
|
|
|
|
2013-09-21 07:06:50 -05:00
|
|
|
if use_rsl {
|
|
|
|
macro_rules! memloop (
|
|
|
|
($arr:expr) => {{
|
|
|
|
for i in range(0, RAND_SIZE_64 / 8).map(|i| i * 8) {
|
|
|
|
a+=$arr[i ]; b+=$arr[i+1];
|
|
|
|
c+=$arr[i+2]; d+=$arr[i+3];
|
|
|
|
e+=$arr[i+4]; f+=$arr[i+5];
|
|
|
|
g+=$arr[i+6]; h+=$arr[i+7];
|
|
|
|
mix!();
|
|
|
|
self.mem[i ]=a; self.mem[i+1]=b;
|
|
|
|
self.mem[i+2]=c; self.mem[i+3]=d;
|
|
|
|
self.mem[i+4]=e; self.mem[i+5]=f;
|
|
|
|
self.mem[i+6]=g; self.mem[i+7]=h;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
|
|
|
|
memloop!(self.rsl);
|
|
|
|
memloop!(self.mem);
|
|
|
|
} else {
|
|
|
|
for i in range(0, RAND_SIZE_64 / 8).map(|i| i * 8) {
|
|
|
|
mix!();
|
|
|
|
self.mem[i ]=a; self.mem[i+1]=b;
|
|
|
|
self.mem[i+2]=c; self.mem[i+3]=d;
|
|
|
|
self.mem[i+4]=e; self.mem[i+5]=f;
|
|
|
|
self.mem[i+6]=g; self.mem[i+7]=h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.isaac64();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Refills the output buffer (`self.rsl`)
|
|
|
|
fn isaac64(&mut self) {
|
|
|
|
self.c += 1;
|
|
|
|
// abbreviations
|
|
|
|
let mut a = self.a;
|
|
|
|
let mut b = self.b + self.c;
|
2014-10-06 18:18:57 -05:00
|
|
|
const MIDPOINT: uint = RAND_SIZE_64 / 2;
|
|
|
|
const MP_VEC: [(uint, uint), .. 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
|
2013-09-21 07:06:50 -05:00
|
|
|
macro_rules! ind (
|
|
|
|
($x:expr) => {
|
2014-08-06 22:08:16 -05:00
|
|
|
*self.mem.unsafe_get(($x as uint >> 3) & (RAND_SIZE_64 - 1))
|
2013-09-21 07:06:50 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
for &(mr_offset, m2_offset) in MP_VEC.iter() {
|
|
|
|
for base in range(0, MIDPOINT / 4).map(|i| i * 4) {
|
2014-06-24 20:57:00 -05:00
|
|
|
|
|
|
|
macro_rules! rngstepp(
|
|
|
|
($j:expr, $shift:expr) => {{
|
|
|
|
let base = base + $j;
|
|
|
|
let mix = a ^ (a << $shift as uint);
|
|
|
|
let mix = if $j == 0 {!mix} else {mix};
|
|
|
|
|
|
|
|
unsafe {
|
2014-08-06 22:08:16 -05:00
|
|
|
let x = *self.mem.unsafe_get(base + mr_offset);
|
|
|
|
a = mix + *self.mem.unsafe_get(base + m2_offset);
|
2014-06-24 20:57:00 -05:00
|
|
|
let y = ind!(x) + a + b;
|
2014-10-15 01:05:01 -05:00
|
|
|
*self.mem.unsafe_mut(base + mr_offset) = y;
|
2014-06-24 20:57:00 -05:00
|
|
|
|
|
|
|
b = ind!(y >> RAND_SIZE_64_LEN) + x;
|
2014-10-15 01:05:01 -05:00
|
|
|
*self.rsl.unsafe_mut(base + mr_offset) = b;
|
2014-06-24 20:57:00 -05:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
macro_rules! rngstepn(
|
|
|
|
($j:expr, $shift:expr) => {{
|
|
|
|
let base = base + $j;
|
|
|
|
let mix = a ^ (a >> $shift as uint);
|
|
|
|
let mix = if $j == 0 {!mix} else {mix};
|
|
|
|
|
|
|
|
unsafe {
|
2014-08-06 22:08:16 -05:00
|
|
|
let x = *self.mem.unsafe_get(base + mr_offset);
|
|
|
|
a = mix + *self.mem.unsafe_get(base + m2_offset);
|
2014-06-24 20:57:00 -05:00
|
|
|
let y = ind!(x) + a + b;
|
2014-10-15 01:05:01 -05:00
|
|
|
*self.mem.unsafe_mut(base + mr_offset) = y;
|
2014-06-24 20:57:00 -05:00
|
|
|
|
|
|
|
b = ind!(y >> RAND_SIZE_64_LEN) + x;
|
2014-10-15 01:05:01 -05:00
|
|
|
*self.rsl.unsafe_mut(base + mr_offset) = b;
|
2014-06-24 20:57:00 -05:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
);
|
2014-06-27 14:30:25 -05:00
|
|
|
rngstepp!(0u, 21);
|
|
|
|
rngstepn!(1u, 5);
|
|
|
|
rngstepp!(2u, 12);
|
|
|
|
rngstepn!(3u, 33);
|
2013-09-21 07:06:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.a = a;
|
|
|
|
self.b = b;
|
|
|
|
self.cnt = RAND_SIZE_64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng for Isaac64Rng {
|
2013-10-08 17:56:57 -05:00
|
|
|
// FIXME #7771: having next_u32 like this should be unnecessary
|
|
|
|
#[inline]
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
self.next_u64() as u32
|
|
|
|
}
|
|
|
|
|
2013-09-21 07:06:50 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
if self.cnt == 0 {
|
|
|
|
// make some more numbers
|
|
|
|
self.isaac64();
|
|
|
|
}
|
|
|
|
self.cnt -= 1;
|
2014-09-08 04:33:37 -05:00
|
|
|
|
|
|
|
// See corresponding location in IsaacRng.next_u32 for
|
|
|
|
// explanation.
|
|
|
|
debug_assert!(self.cnt < RAND_SIZE_64)
|
|
|
|
self.rsl[(self.cnt % RAND_SIZE_64) as uint]
|
2013-09-21 07:06:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng {
|
|
|
|
fn reseed(&mut self, seed: &'a [u64]) {
|
2013-09-29 10:29:28 -05:00
|
|
|
// make the seed into [seed[0], seed[1], ..., seed[seed.len()
|
|
|
|
// - 1], 0, 0, ...], to fill rng.rsl.
|
2014-11-06 11:32:37 -06:00
|
|
|
let seed_iter = seed.iter().map(|&x| x).chain(repeat(0u64));
|
2013-09-29 10:29:28 -05:00
|
|
|
|
2014-09-14 22:27:36 -05:00
|
|
|
for (rsl_elem, seed_elem) in self.rsl.iter_mut().zip(seed_iter) {
|
2013-09-29 10:29:28 -05:00
|
|
|
*rsl_elem = seed_elem;
|
|
|
|
}
|
|
|
|
self.cnt = 0;
|
|
|
|
self.a = 0;
|
|
|
|
self.b = 0;
|
|
|
|
self.c = 0;
|
|
|
|
|
|
|
|
self.init(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an ISAAC random number generator with a seed. This can
|
|
|
|
/// be any length, although the maximum number of elements used is
|
|
|
|
/// 256 and any more will be silently ignored. A generator
|
|
|
|
/// constructed with a given seed will generate the same sequence
|
|
|
|
/// of values as all other generators constructed with that seed.
|
2013-12-10 01:16:18 -06:00
|
|
|
fn from_seed(seed: &'a [u64]) -> Isaac64Rng {
|
2013-10-08 08:21:26 -05:00
|
|
|
let mut rng = EMPTY_64;
|
2013-09-29 10:29:28 -05:00
|
|
|
rng.reseed(seed);
|
|
|
|
rng
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 03:39:37 -05:00
|
|
|
impl Rand for Isaac64Rng {
|
|
|
|
fn rand<R: Rng>(other: &mut R) -> Isaac64Rng {
|
|
|
|
let mut ret = EMPTY_64;
|
|
|
|
unsafe {
|
2014-11-20 12:11:15 -06:00
|
|
|
let ptr = ret.rsl.as_mut_ptr() as *mut u8;
|
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 03:39:37 -05:00
|
|
|
|
2014-11-20 12:11:15 -06:00
|
|
|
let slice = slice::from_raw_mut_buf(&ptr, (RAND_SIZE_64 * 8) as uint);
|
|
|
|
other.fill_bytes(slice);
|
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 03:39:37 -05:00
|
|
|
}
|
|
|
|
ret.cnt = 0;
|
|
|
|
ret.a = 0;
|
|
|
|
ret.b = 0;
|
|
|
|
ret.c = 0;
|
|
|
|
|
|
|
|
ret.init(true);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 07:06:50 -05: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 03:39:37 -05:00
|
|
|
use std::prelude::*;
|
|
|
|
|
|
|
|
use core::iter::order;
|
|
|
|
use {Rng, SeedableRng};
|
2014-03-01 18:23:04 -06:00
|
|
|
use super::{IsaacRng, Isaac64Rng};
|
2013-09-21 07:06:50 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-10-01 11:23:22 -05:00
|
|
|
fn test_rng_32_rand_seeded() {
|
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 03:39:37 -05:00
|
|
|
let s = ::test::rng().gen_iter::<u32>().take(256).collect::<Vec<u32>>();
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut ra: IsaacRng = SeedableRng::from_seed(s.as_slice());
|
|
|
|
let mut rb: IsaacRng = SeedableRng::from_seed(s.as_slice());
|
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 03:39:37 -05:00
|
|
|
assert!(order::equals(ra.gen_ascii_chars().take(100),
|
|
|
|
rb.gen_ascii_chars().take(100)));
|
2013-10-01 11:23:22 -05:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_rng_64_rand_seeded() {
|
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 03:39:37 -05:00
|
|
|
let s = ::test::rng().gen_iter::<u64>().take(256).collect::<Vec<u64>>();
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut ra: Isaac64Rng = SeedableRng::from_seed(s.as_slice());
|
|
|
|
let mut rb: Isaac64Rng = SeedableRng::from_seed(s.as_slice());
|
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 03:39:37 -05:00
|
|
|
assert!(order::equals(ra.gen_ascii_chars().take(100),
|
|
|
|
rb.gen_ascii_chars().take(100)));
|
2013-09-21 07:06:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-10-01 11:23:22 -05:00
|
|
|
fn test_rng_32_seeded() {
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[1, 23, 456, 7890, 12345];
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut ra: IsaacRng = SeedableRng::from_seed(seed);
|
|
|
|
let mut rb: IsaacRng = SeedableRng::from_seed(seed);
|
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 03:39:37 -05:00
|
|
|
assert!(order::equals(ra.gen_ascii_chars().take(100),
|
|
|
|
rb.gen_ascii_chars().take(100)));
|
2013-10-01 11:23:22 -05:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_rng_64_seeded() {
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[1, 23, 456, 7890, 12345];
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut ra: Isaac64Rng = SeedableRng::from_seed(seed);
|
|
|
|
let mut rb: Isaac64Rng = SeedableRng::from_seed(seed);
|
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 03:39:37 -05:00
|
|
|
assert!(order::equals(ra.gen_ascii_chars().take(100),
|
|
|
|
rb.gen_ascii_chars().take(100)));
|
2013-09-21 07:06:50 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 11:23:22 -05:00
|
|
|
#[test]
|
|
|
|
fn test_rng_32_reseed() {
|
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 03:39:37 -05:00
|
|
|
let s = ::test::rng().gen_iter::<u32>().take(256).collect::<Vec<u32>>();
|
2013-10-01 11:23:22 -05:00
|
|
|
let mut r: IsaacRng = SeedableRng::from_seed(s.as_slice());
|
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 03:39:37 -05:00
|
|
|
let string1: String = r.gen_ascii_chars().take(100).collect();
|
2013-10-01 11:23:22 -05:00
|
|
|
|
2014-03-27 07:00:46 -05:00
|
|
|
r.reseed(s.as_slice());
|
2013-10-01 11:23:22 -05: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 03:39:37 -05:00
|
|
|
let string2: String = r.gen_ascii_chars().take(100).collect();
|
2013-10-01 11:23:22 -05:00
|
|
|
assert_eq!(string1, string2);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_rng_64_reseed() {
|
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 03:39:37 -05:00
|
|
|
let s = ::test::rng().gen_iter::<u64>().take(256).collect::<Vec<u64>>();
|
2013-10-01 11:23:22 -05:00
|
|
|
let mut r: Isaac64Rng = SeedableRng::from_seed(s.as_slice());
|
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 03:39:37 -05:00
|
|
|
let string1: String = r.gen_ascii_chars().take(100).collect();
|
2013-10-01 11:23:22 -05:00
|
|
|
|
2014-03-27 07:00:46 -05:00
|
|
|
r.reseed(s.as_slice());
|
2013-10-01 11:23:22 -05: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 03:39:37 -05:00
|
|
|
let string2: String = r.gen_ascii_chars().take(100).collect();
|
2013-10-01 11:23:22 -05:00
|
|
|
assert_eq!(string1, string2);
|
|
|
|
}
|
|
|
|
|
2013-09-21 07:06:50 -05:00
|
|
|
#[test]
|
2013-09-29 10:29:28 -05:00
|
|
|
fn test_rng_32_true_values() {
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[1, 23, 456, 7890, 12345];
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut ra: IsaacRng = SeedableRng::from_seed(seed);
|
2013-09-21 07:06:50 -05:00
|
|
|
// Regression test that isaac is actually using the above vector
|
2014-03-27 07:00:46 -05:00
|
|
|
let v = Vec::from_fn(10, |_| ra.next_u32());
|
2013-09-29 10:29:28 -05:00
|
|
|
assert_eq!(v,
|
2014-03-27 07:00:46 -05:00
|
|
|
vec!(2558573138, 873787463, 263499565, 2103644246, 3595684709,
|
|
|
|
4203127393, 264982119, 2765226902, 2737944514, 3900253796));
|
2013-09-29 10:29:28 -05:00
|
|
|
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[12345, 67890, 54321, 9876];
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rb: IsaacRng = SeedableRng::from_seed(seed);
|
|
|
|
// skip forward to the 10000th number
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 10000) { rb.next_u32(); }
|
2013-09-29 10:29:28 -05:00
|
|
|
|
2014-03-27 07:00:46 -05:00
|
|
|
let v = Vec::from_fn(10, |_| rb.next_u32());
|
2013-09-29 10:29:28 -05:00
|
|
|
assert_eq!(v,
|
2014-03-27 07:00:46 -05:00
|
|
|
vec!(3676831399, 3183332890, 2834741178, 3854698763, 2717568474,
|
|
|
|
1576568959, 3507990155, 179069555, 141456972, 2478885421));
|
2013-09-29 10:29:28 -05:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_rng_64_true_values() {
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[1, 23, 456, 7890, 12345];
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut ra: Isaac64Rng = SeedableRng::from_seed(seed);
|
2013-09-21 07:06:50 -05:00
|
|
|
// Regression test that isaac is actually using the above vector
|
2014-03-27 07:00:46 -05:00
|
|
|
let v = Vec::from_fn(10, |_| ra.next_u64());
|
2013-09-29 10:29:28 -05:00
|
|
|
assert_eq!(v,
|
2014-03-27 07:00:46 -05:00
|
|
|
vec!(547121783600835980, 14377643087320773276, 17351601304698403469,
|
|
|
|
1238879483818134882, 11952566807690396487, 13970131091560099343,
|
|
|
|
4469761996653280935, 15552757044682284409, 6860251611068737823,
|
|
|
|
13722198873481261842));
|
2013-09-29 10:29:28 -05:00
|
|
|
|
2014-08-04 07:19:02 -05:00
|
|
|
let seed: &[_] = &[12345, 67890, 54321, 9876];
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rb: Isaac64Rng = SeedableRng::from_seed(seed);
|
|
|
|
// skip forward to the 10000th number
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 10000) { rb.next_u64(); }
|
2013-09-29 10:29:28 -05:00
|
|
|
|
2014-03-27 07:00:46 -05:00
|
|
|
let v = Vec::from_fn(10, |_| rb.next_u64());
|
2013-09-29 10:29:28 -05:00
|
|
|
assert_eq!(v,
|
2014-03-27 07:00:46 -05:00
|
|
|
vec!(18143823860592706164, 8491801882678285927, 2699425367717515619,
|
|
|
|
17196852593171130876, 2606123525235546165, 15790932315217671084,
|
|
|
|
596345674630742204, 9947027391921273664, 11788097613744130851,
|
|
|
|
10391409374914919106));
|
2013-09-21 07:06:50 -05:00
|
|
|
}
|
|
|
|
}
|