2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Random number generation
|
2012-03-15 20:58:14 -05:00
|
|
|
|
2012-09-27 13:14:59 -05:00
|
|
|
// NB: transitional, de-mode-ing.
|
2012-10-03 16:52:09 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-09-27 13:14:59 -05:00
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use int;
|
2013-01-08 21:37:25 -06:00
|
|
|
use prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use str;
|
|
|
|
use task;
|
|
|
|
use u32;
|
|
|
|
use uint;
|
|
|
|
use util;
|
|
|
|
use vec;
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
#[allow(non_camel_case_types)] // runtime type
|
2012-02-01 04:45:23 -06:00
|
|
|
enum rctx {}
|
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
2012-07-03 18:11:00 -05:00
|
|
|
extern mod rustrt {
|
2012-06-29 18:26:56 -05:00
|
|
|
fn rand_seed() -> ~[u8];
|
2012-02-01 04:45:23 -06:00
|
|
|
fn rand_new() -> *rctx;
|
2012-10-02 14:19:04 -05:00
|
|
|
fn rand_new_seeded2(&&seed: ~[u8]) -> *rctx;
|
2012-02-01 04:45:23 -06:00
|
|
|
fn rand_next(c: *rctx) -> u32;
|
|
|
|
fn rand_free(c: *rctx);
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A random number generator
|
2012-09-26 18:01:40 -05:00
|
|
|
pub trait Rng {
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return the next random integer
|
2011-10-26 18:24:31 -05:00
|
|
|
fn next() -> u32;
|
2012-05-17 13:52:49 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A value with a particular weight compared to other values
|
2012-09-26 18:01:40 -05:00
|
|
|
pub type Weighted<T> = { weight: uint, item: T };
|
2012-05-19 13:25:45 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Extension methods for random number generators
|
2012-08-14 15:38:35 -05:00
|
|
|
impl Rng {
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random int
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_int() -> int {
|
|
|
|
self.gen_i64() as int
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Return an int randomly chosen from the range [start, end),
|
|
|
|
* failing if start >= end
|
|
|
|
*/
|
2012-05-21 20:57:28 -05:00
|
|
|
fn gen_int_range(start: int, end: int) -> int {
|
|
|
|
assert start < end;
|
|
|
|
start + int::abs(self.gen_int() % (end - start))
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random i8
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_i8() -> i8 {
|
|
|
|
self.next() as i8
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random i16
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_i16() -> i16 {
|
|
|
|
self.next() as i16
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random i32
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_i32() -> i32 {
|
|
|
|
self.next() as i32
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random i64
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_i64() -> i64 {
|
|
|
|
(self.next() as i64 << 32) | self.next() as i64
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random uint
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_uint() -> uint {
|
2012-05-21 19:25:33 -05:00
|
|
|
self.gen_u64() as uint
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Return a uint randomly chosen from the range [start, end),
|
|
|
|
* failing if start >= end
|
|
|
|
*/
|
2012-05-21 20:57:28 -05:00
|
|
|
fn gen_uint_range(start: uint, end: uint) -> uint {
|
|
|
|
assert start < end;
|
|
|
|
start + (self.gen_uint() % (end - start))
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random u8
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_u8() -> u8 {
|
|
|
|
self.next() as u8
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random u16
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_u16() -> u16 {
|
|
|
|
self.next() as u16
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random u32
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_u32() -> u32 {
|
|
|
|
self.next()
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random u64
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_u64() -> u64 {
|
|
|
|
(self.next() as u64 << 32) | self.next() as u64
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:28:32 -05:00
|
|
|
/// Return a random float in the interval [0,1]
|
2012-05-17 13:52:49 -05:00
|
|
|
fn gen_float() -> float {
|
2012-05-19 13:25:45 -05:00
|
|
|
self.gen_f64() as float
|
2012-05-17 13:52:49 -05:00
|
|
|
}
|
2011-11-08 22:35:15 -06:00
|
|
|
|
2012-07-16 21:28:32 -05:00
|
|
|
/// Return a random f32 in the interval [0,1]
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_f32() -> f32 {
|
|
|
|
self.gen_f64() as f32
|
|
|
|
}
|
|
|
|
|
2012-07-16 21:28:32 -05:00
|
|
|
/// Return a random f64 in the interval [0,1]
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_f64() -> f64 {
|
|
|
|
let u1 = self.next() as f64;
|
|
|
|
let u2 = self.next() as f64;
|
|
|
|
let u3 = self.next() as f64;
|
2012-07-16 21:28:32 -05:00
|
|
|
const scale : f64 = (u32::max_value as f64) + 1.0f64;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ((u1 / scale + u2) / scale + u3) / scale;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random char
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_char() -> char {
|
|
|
|
self.next() as char
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Return a char randomly chosen from chars, failing if chars is empty
|
|
|
|
*/
|
2012-09-27 13:14:59 -05:00
|
|
|
fn gen_char_from(chars: &str) -> char {
|
2012-05-19 13:25:45 -05:00
|
|
|
assert !chars.is_empty();
|
|
|
|
self.choose(str::chars(chars))
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random bool
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_bool() -> bool {
|
|
|
|
self.next() & 1u32 == 1u32
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a bool with a 1 in n chance of true
|
2012-05-19 13:25:45 -05:00
|
|
|
fn gen_weighted_bool(n: uint) -> bool {
|
|
|
|
if n == 0u {
|
|
|
|
true
|
|
|
|
} else {
|
2012-05-21 20:57:28 -05:00
|
|
|
self.gen_uint_range(1u, n + 1u) == 1u
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Return a random string of the specified length composed of A-Z,a-z,0-9
|
|
|
|
*/
|
2012-07-14 00:57:48 -05:00
|
|
|
fn gen_str(len: uint) -> ~str {
|
|
|
|
let charset = ~"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
2012-07-06 13:35:46 -05:00
|
|
|
abcdefghijklmnopqrstuvwxyz\
|
|
|
|
0123456789";
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut s = ~"";
|
2012-05-17 13:52:49 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
while (i < len) {
|
2012-05-19 13:25:45 -05:00
|
|
|
s = s + str::from_char(self.gen_char_from(charset));
|
2012-05-17 13:52:49 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-09-10 18:31:00 -05:00
|
|
|
move s
|
2012-05-17 13:52:49 -05:00
|
|
|
}
|
2011-12-26 21:31:25 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Return a random byte string of the specified length
|
2012-06-29 18:26:56 -05:00
|
|
|
fn gen_bytes(len: uint) -> ~[u8] {
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::from_fn(len) |_i| {
|
2012-05-19 13:25:45 -05:00
|
|
|
self.gen_u8()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Choose an item randomly, failing if values is empty
|
2012-09-07 16:52:28 -05:00
|
|
|
fn choose<T:Copy>(values: &[T]) -> T {
|
2012-05-19 13:25:45 -05:00
|
|
|
self.choose_option(values).get()
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
/// Choose Some(item) randomly, returning None if values is empty
|
2012-09-07 16:52:28 -05:00
|
|
|
fn choose_option<T:Copy>(values: &[T]) -> Option<T> {
|
2012-05-19 13:25:45 -05:00
|
|
|
if values.is_empty() {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-05-19 13:25:45 -05:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(values[self.gen_uint_range(0u, values.len())])
|
2012-05-17 13:52:49 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-19 13:25:45 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Choose an item respecting the relative weights, failing if the sum of
|
|
|
|
* the weights is 0
|
|
|
|
*/
|
2012-09-07 16:52:28 -05:00
|
|
|
fn choose_weighted<T: Copy>(v : &[Weighted<T>]) -> T {
|
2012-05-19 13:25:45 -05:00
|
|
|
self.choose_weighted_option(v).get()
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2012-08-20 14:23:37 -05:00
|
|
|
* Choose Some(item) respecting the relative weights, returning none if
|
2012-07-04 16:53:12 -05:00
|
|
|
* the sum of the weights is 0
|
|
|
|
*/
|
2012-09-07 16:52:28 -05:00
|
|
|
fn choose_weighted_option<T:Copy>(v: &[Weighted<T>]) -> Option<T> {
|
2012-05-19 13:25:45 -05:00
|
|
|
let mut total = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
for v.each |item| {
|
2012-05-19 13:25:45 -05:00
|
|
|
total += item.weight;
|
|
|
|
}
|
|
|
|
if total == 0u {
|
2012-08-20 14:23:37 -05:00
|
|
|
return None;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
2012-05-21 20:57:28 -05:00
|
|
|
let chosen = self.gen_uint_range(0u, total);
|
2012-05-19 13:25:45 -05:00
|
|
|
let mut so_far = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
for v.each |item| {
|
2012-05-19 13:25:45 -05:00
|
|
|
so_far += item.weight;
|
|
|
|
if so_far > chosen {
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(item.item);
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 16:27:25 -05:00
|
|
|
util::unreachable();
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Return a vec containing copies of the items, in order, where
|
|
|
|
* the weight of the item determines how many copies there are
|
|
|
|
*/
|
2012-09-07 16:52:28 -05:00
|
|
|
fn weighted_vec<T:Copy>(v: &[Weighted<T>]) -> ~[T] {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut r = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for v.each |item| {
|
|
|
|
for uint::range(0u, item.weight) |_i| {
|
2012-09-26 19:33:34 -05:00
|
|
|
r.push(item.item);
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-10 18:31:00 -05:00
|
|
|
move r
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Shuffle a vec
|
2012-09-07 16:52:28 -05:00
|
|
|
fn shuffle<T:Copy>(values: &[T]) -> ~[T] {
|
2012-08-27 21:55:18 -05:00
|
|
|
let mut m = vec::from_slice(values);
|
2012-05-19 13:25:45 -05:00
|
|
|
self.shuffle_mut(m);
|
2012-09-10 18:31:00 -05:00
|
|
|
move m
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Shuffle a mutable vec in place
|
2012-08-27 21:55:18 -05:00
|
|
|
fn shuffle_mut<T>(values: &[mut T]) {
|
2012-05-19 13:25:45 -05:00
|
|
|
let mut i = values.len();
|
|
|
|
while i >= 2u {
|
|
|
|
// invariant: elements with index >= i have been locked in place.
|
|
|
|
i -= 1u;
|
|
|
|
// lock element i in place.
|
2012-05-21 20:57:28 -05:00
|
|
|
vec::swap(values, i, self.gen_uint_range(0u, i + 1u));
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 05:54:39 -06:00
|
|
|
}
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2012-08-15 20:46:55 -05:00
|
|
|
struct RandRes {
|
2012-09-06 21:40:15 -05:00
|
|
|
c: *rctx,
|
2012-06-21 23:46:43 -05:00
|
|
|
drop { rustrt::rand_free(self.c); }
|
|
|
|
}
|
2012-05-17 13:52:49 -05:00
|
|
|
|
2012-09-04 17:23:28 -05:00
|
|
|
fn RandRes(c: *rctx) -> RandRes {
|
|
|
|
RandRes {
|
|
|
|
c: c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
impl @RandRes: Rng {
|
2012-08-01 19:30:05 -05:00
|
|
|
fn next() -> u32 { return rustrt::rand_next((*self).c); }
|
2012-05-20 08:06:54 -05:00
|
|
|
}
|
2012-05-17 13:52:49 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create a new random seed for seeded_rng
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn seed() -> ~[u8] {
|
2012-05-20 08:06:54 -05:00
|
|
|
rustrt::rand_seed()
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create a random number generator with a system specified seed
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn Rng() -> Rng {
|
2012-08-14 15:38:35 -05:00
|
|
|
@RandRes(rustrt::rand_new()) as Rng
|
2010-07-25 23:45:09 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Create a random number generator using the specified seed. A generator
|
|
|
|
* constructed with a given seed will generate the same sequence of values as
|
|
|
|
* all other generators constructed with the same seed. The seed may be any
|
|
|
|
* length.
|
|
|
|
*/
|
2012-09-27 13:14:59 -05:00
|
|
|
pub fn seeded_rng(seed: &~[u8]) -> Rng {
|
2012-10-02 14:19:04 -05:00
|
|
|
@RandRes(rustrt::rand_new_seeded2(*seed)) as Rng
|
2012-05-20 08:06:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
type XorShiftState = {
|
2012-05-30 18:31:16 -05:00
|
|
|
mut x: u32,
|
|
|
|
mut y: u32,
|
|
|
|
mut z: u32,
|
|
|
|
mut w: u32
|
|
|
|
};
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
impl XorShiftState: Rng {
|
2012-05-30 18:31:16 -05:00
|
|
|
fn next() -> u32 {
|
|
|
|
let x = self.x;
|
|
|
|
let mut t = x ^ (x << 11);
|
|
|
|
self.x = self.y;
|
|
|
|
self.y = self.z;
|
|
|
|
self.z = self.w;
|
|
|
|
let w = self.w;
|
|
|
|
self.w = w ^ (w >> 19) ^ (t ^ (t >> 8));
|
|
|
|
self.w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-23 16:37:44 -06:00
|
|
|
pub pure fn xorshift() -> Rng {
|
2012-05-30 18:31:16 -05:00
|
|
|
// constants taken from http://en.wikipedia.org/wiki/Xorshift
|
|
|
|
seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32)
|
|
|
|
}
|
|
|
|
|
2012-12-23 16:37:44 -06:00
|
|
|
pub pure fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> Rng {
|
2012-08-14 15:38:35 -05:00
|
|
|
{mut x: x, mut y: y, mut z: z, mut w: w} as Rng
|
2012-05-30 18:31:16 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 22:29:34 -05:00
|
|
|
|
|
|
|
// used to make space in TLS for a random number generator
|
2012-10-03 16:52:09 -05:00
|
|
|
fn tls_rng_state(_v: @RandRes) {}
|
2012-10-01 22:29:34 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gives back a lazily initialized task-local random number generator,
|
|
|
|
* seeded by the system. Intended to be used in method chaining style, ie
|
|
|
|
* task_rng().gen_int().
|
|
|
|
*/
|
|
|
|
pub fn task_rng() -> Rng {
|
|
|
|
let r : Option<@RandRes>;
|
|
|
|
unsafe {
|
|
|
|
r = task::local_data::local_data_get(tls_rng_state);
|
|
|
|
}
|
|
|
|
match r {
|
|
|
|
None => {
|
|
|
|
let rng = @RandRes(rustrt::rand_new());
|
|
|
|
unsafe {
|
|
|
|
task::local_data::local_data_set(tls_rng_state, rng);
|
|
|
|
}
|
|
|
|
rng as Rng
|
|
|
|
}
|
|
|
|
Some(rng) => rng as Rng
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:15:14 -05:00
|
|
|
/**
|
|
|
|
* Returns a random uint, using the task's based random number generator.
|
|
|
|
*/
|
|
|
|
pub fn random() -> uint {
|
|
|
|
task_rng().gen_uint()
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:29:34 -05:00
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub mod tests {
|
2013-01-08 21:37:25 -06:00
|
|
|
use debug;
|
|
|
|
use option::{None, Option, Some};
|
2012-12-27 19:53:04 -06:00
|
|
|
use rand;
|
|
|
|
|
2012-05-20 08:06:54 -05:00
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn rng_seeded() {
|
2012-05-20 08:06:54 -05:00
|
|
|
let seed = rand::seed();
|
2012-09-27 13:14:59 -05:00
|
|
|
let ra = rand::seeded_rng(&seed);
|
|
|
|
let rb = rand::seeded_rng(&seed);
|
2012-05-20 08:06:54 -05:00
|
|
|
assert ra.gen_str(100u) == rb.gen_str(100u);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn rng_seeded_custom_seed() {
|
2012-05-20 08:06:54 -05:00
|
|
|
// much shorter than generated seeds which are 1024 bytes
|
2012-06-29 18:26:56 -05:00
|
|
|
let seed = ~[2u8, 32u8, 4u8, 32u8, 51u8];
|
2012-09-27 13:14:59 -05:00
|
|
|
let ra = rand::seeded_rng(&seed);
|
|
|
|
let rb = rand::seeded_rng(&seed);
|
2012-05-20 08:06:54 -05:00
|
|
|
assert ra.gen_str(100u) == rb.gen_str(100u);
|
|
|
|
}
|
|
|
|
|
2012-06-05 01:26:06 -05:00
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn rng_seeded_custom_seed2() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let seed = ~[2u8, 32u8, 4u8, 32u8, 51u8];
|
2012-09-27 13:14:59 -05:00
|
|
|
let ra = rand::seeded_rng(&seed);
|
2012-06-05 01:26:06 -05:00
|
|
|
// Regression test that isaac is actually using the above vector
|
|
|
|
let r = ra.next();
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("%?", r);
|
2012-06-05 01:26:06 -05:00
|
|
|
assert r == 890007737u32 // on x86_64
|
|
|
|
|| r == 2935188040u32; // on x86
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_int_range() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-05-21 20:57:28 -05:00
|
|
|
let a = r.gen_int_range(-3, 42);
|
|
|
|
assert a >= -3 && a < 42;
|
|
|
|
assert r.gen_int_range(0, 1) == 0;
|
|
|
|
assert r.gen_int_range(-12, -11) == -12;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[ignore(cfg(windows))]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_int_from_fail() {
|
2012-08-27 16:22:25 -05:00
|
|
|
rand::Rng().gen_int_range(5, -2);
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_uint_range() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-05-21 20:57:28 -05:00
|
|
|
let a = r.gen_uint_range(3u, 42u);
|
|
|
|
assert a >= 3u && a < 42u;
|
|
|
|
assert r.gen_uint_range(0u, 1u) == 0u;
|
|
|
|
assert r.gen_uint_range(12u, 13u) == 12u;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[ignore(cfg(windows))]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_uint_range_fail() {
|
2012-08-27 16:22:25 -05:00
|
|
|
rand::Rng().gen_uint_range(5u, 2u);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_float() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-05-17 13:52:49 -05:00
|
|
|
let a = r.gen_float();
|
|
|
|
let b = r.gen_float();
|
|
|
|
log(debug, (a, b));
|
|
|
|
}
|
|
|
|
|
2012-05-19 13:25:45 -05:00
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_weighted_bool() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-05-19 13:25:45 -05:00
|
|
|
assert r.gen_weighted_bool(0u) == true;
|
|
|
|
assert r.gen_weighted_bool(1u) == true;
|
|
|
|
}
|
|
|
|
|
2012-05-17 13:52:49 -05:00
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_str() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-01-17 21:05:07 -06:00
|
|
|
log(debug, r.gen_str(10u));
|
|
|
|
log(debug, r.gen_str(10u));
|
|
|
|
log(debug, r.gen_str(10u));
|
2012-05-17 13:52:49 -05:00
|
|
|
assert r.gen_str(0u).len() == 0u;
|
|
|
|
assert r.gen_str(10u).len() == 10u;
|
|
|
|
assert r.gen_str(16u).len() == 16u;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn gen_bytes() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-05-17 13:52:49 -05:00
|
|
|
assert r.gen_bytes(0u).len() == 0u;
|
|
|
|
assert r.gen_bytes(10u).len() == 10u;
|
|
|
|
assert r.gen_bytes(16u).len() == 16u;
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-05-19 13:25:45 -05:00
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn choose() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-08-27 21:55:18 -05:00
|
|
|
assert r.choose([1, 1, 1]) == 1;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn choose_option() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-08-27 18:26:35 -05:00
|
|
|
let x: Option<int> = r.choose_option([]);
|
|
|
|
assert x.is_none();
|
2012-08-27 21:55:18 -05:00
|
|
|
assert r.choose_option([1, 1, 1]) == Some(1);
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn choose_weighted() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-06-29 18:26:56 -05:00
|
|
|
assert r.choose_weighted(~[{weight: 1u, item: 42}]) == 42;
|
|
|
|
assert r.choose_weighted(~[
|
2012-05-19 13:25:45 -05:00
|
|
|
{weight: 0u, item: 42},
|
|
|
|
{weight: 1u, item: 43}
|
2012-06-29 18:26:56 -05:00
|
|
|
]) == 43;
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn choose_weighted_option() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-06-29 18:26:56 -05:00
|
|
|
assert r.choose_weighted_option(~[{weight: 1u, item: 42}]) ==
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(42);
|
2012-06-29 18:26:56 -05:00
|
|
|
assert r.choose_weighted_option(~[
|
2012-05-19 13:25:45 -05:00
|
|
|
{weight: 0u, item: 42},
|
|
|
|
{weight: 1u, item: 43}
|
2012-08-20 14:23:37 -05:00
|
|
|
]) == Some(43);
|
2012-08-27 18:26:35 -05:00
|
|
|
let v: Option<int> = r.choose_weighted_option([]);
|
|
|
|
assert v.is_none();
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn weighted_vec() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-06-29 18:26:56 -05:00
|
|
|
let empty: ~[int] = ~[];
|
|
|
|
assert r.weighted_vec(~[]) == empty;
|
|
|
|
assert r.weighted_vec(~[
|
2012-05-19 13:25:45 -05:00
|
|
|
{weight: 0u, item: 3u},
|
|
|
|
{weight: 1u, item: 2u},
|
|
|
|
{weight: 2u, item: 1u}
|
2012-06-29 18:26:56 -05:00
|
|
|
]) == ~[2u, 1u, 1u];
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 18:01:40 -05:00
|
|
|
pub fn shuffle() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-06-29 18:26:56 -05:00
|
|
|
let empty: ~[int] = ~[];
|
|
|
|
assert r.shuffle(~[]) == empty;
|
|
|
|
assert r.shuffle(~[1, 1, 1]) == ~[1, 1, 1];
|
2012-05-19 13:25:45 -05:00
|
|
|
}
|
2012-10-01 22:48:33 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn task_rng() {
|
|
|
|
let r = rand::task_rng();
|
|
|
|
r.gen_int();
|
|
|
|
assert r.shuffle(~[1, 1, 1]) == ~[1, 1, 1];
|
|
|
|
assert r.gen_uint_range(0u, 1u) == 0u;
|
|
|
|
}
|
2012-10-02 16:15:14 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn random() {
|
|
|
|
// not sure how to test this aside from just getting a number
|
|
|
|
let _n : uint = rand::random();
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 17:44:13 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|