Skip to content

auto: rand::Rand: a trait implemented by types that can be randomly generated #4801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,102 @@ use uint;
use util;
use vec;

/// A type that can be randomly generated using an RNG
pub trait Rand {
static fn rand(rng: rand::Rng) -> Self;
}

impl int: Rand {
static fn rand(rng: rand::Rng) -> int {
rng.gen_int()
}
}

impl i8: Rand {
static fn rand(rng: rand::Rng) -> i8 {
rng.gen_i8()
}
}

impl i16: Rand {
static fn rand(rng: rand::Rng) -> i16 {
rng.gen_i16()
}
}

impl i32: Rand {
static fn rand(rng: rand::Rng) -> i32 {
rng.gen_i32()
}
}

impl i64: Rand {
static fn rand(rng: rand::Rng) -> i64 {
rng.gen_i64()
}
}

impl u8: Rand {
static fn rand(rng: rand::Rng) -> u8 {
rng.gen_u8()
}
}

impl u16: Rand {
static fn rand(rng: rand::Rng) -> u16 {
rng.gen_u16()
}
}

impl u32: Rand {
static fn rand(rng: rand::Rng) -> u32 {
rng.gen_u32()
}
}

impl u64: Rand {
static fn rand(rng: rand::Rng) -> u64 {
rng.gen_u64()
}
}

impl float: Rand {
static fn rand(rng: rand::Rng) -> float {
rng.gen_float()
}
}

impl f32: Rand {
static fn rand(rng: rand::Rng) -> f32 {
rng.gen_f32()
}
}

impl f64: Rand {
static fn rand(rng: rand::Rng) -> f64 {
rng.gen_f64()
}
}

impl char: Rand {
static fn rand(rng: rand::Rng) -> char {
rng.gen_char()
}
}

impl bool: Rand {
static fn rand(rng: rand::Rng) -> bool {
rng.gen_bool()
}
}

impl<T: Rand> Option<T>: Rand {
static fn rand(rng: rand::Rng) -> Option<T> {
if rng.gen_bool() { Some(Rand::rand(rng)) }
else { None }
}
}

#[allow(non_camel_case_types)] // runtime type
enum rctx {}

Expand All @@ -49,6 +145,10 @@ pub struct Weighted<T> {

/// Extension methods for random number generators
impl Rng {
/// Return a random value for a Rand type
fn gen<T: Rand>() -> T {
Rand::rand(self)
}

/// Return a random int
fn gen_int() -> int {
Expand Down