Struct Rand

Source
pub struct Rand<S>
where S: Source,
{ /* private fields */ }

Implementations§

Source§

impl<S> Rand<S>
where S: Source,

Source

pub fn new(src: S) -> Rand<S>

Creates a new Rand that uses random values from src to generate other random values.

§Examples
use srand::Rand;
use srand::RngSource;

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    Ok(())
}
Source

pub fn seed(&mut self, seed: i64)

Seed uses the provided seed value to initialize the generator to a deterministic state.

Source

pub fn int64(&mut self) -> i64

Returns a non-negative pseudo-random 63-bit integer as an i64.

§Examples
use srand::{Rand,RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.int64());
    Ok(())
}
Source

pub fn uint64(&mut self) -> u64

Returns a pseudo-random 64-bit value as a u64.

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.uint64());
    Ok(())
}
Source

pub fn int32(&mut self) -> i32

i32 returns a non-negative pseudo-random 31-bit integer as an i32.

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.int32());
    Ok(())
}
Source

pub fn uint32(&mut self) -> u32

Returns a pseudo-random 32-bit value as a u32.

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.uint32());
    Ok(())
}
Source

pub fn int32n(&mut self, n: i32) -> i32

Returns an i32, a non-negative pseudo-random number in [0,n). It panics if n <= 0.

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.int32n(90000001));
    Ok(())
}
Source

pub fn int64n(&mut self, n: i64) -> i64

Returns an i64, a non-negative pseudo-random number in [0,n). It panics if n <= 0.

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.int64n(90000001));
    Ok(())
}
Source

pub fn float32(&mut self) -> f32

Returns a f32, a pseudo-random number in [0.0,1.0).

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.float32());
    Ok(())
}
Source

pub fn float64(&mut self) -> f64

Returns a f64, a pseudo-random number in [0.0,1.0).

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    println!("n: {}", r.float64());
    Ok(())
}
Source

pub fn shuffle<T>(&mut self, array: &mut Vec<T>)

Shuffle pseudo-randomizes the order of elements.

§Examples
use srand::{Rand, RngSource};

fn main() -> std::io::Result<()> {
    let mut r: Rand<_> = Rand::new(RngSource::new(1));
    let mut v = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    println!("result: {:?}", r.shuffle(&mut v));
    println!("shuffled array: {:?}", v);
    Ok(())
}

Trait Implementations§

Source§

impl<S> Clone for Rand<S>
where S: Clone + Sized + Source,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S> Send for Rand<S>
where S: Send + Source,

Source§

impl<S> Sync for Rand<S>
where S: Sync + Source,

Auto Trait Implementations§

§

impl<S> Freeze for Rand<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Rand<S>
where S: RefUnwindSafe,

§

impl<S> Unpin for Rand<S>
where S: Unpin,

§

impl<S> UnwindSafe for Rand<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.