Randomizer

Struct Randomizer 

Source
pub struct Randomizer {
    pub rng: RefCell<Box<dyn RngCore + Send>>,
    pub seed: u64,
}
Expand description

Struct for managing random number generation, allowing seed control for reproducibility.

Fields§

§rng: RefCell<Box<dyn RngCore + Send>>§seed: u64

Implementations§

Source§

impl Randomizer

Source

pub fn with_seed(seed: u64) -> Self

Create a new Randomizer with a specified seed.

§Example:
use crazy_train::Randomizer;
let randomizer = Randomizer::with_seed(42);
Source

pub fn number_between(&self, min: u32, max: u32) -> u32

Generate a random number between the specified minimum and maximum values (inclusive).

§Example:
use crazy_train::Randomizer;
let randomizer = Randomizer::with_seed(42);
assert_eq!(randomizer.number_between(1,10), 7);
Source

pub fn bool(&self) -> bool

Generate a random boolean value (true or false).

§Example:
use crazy_train::Randomizer;
let randomizer = Randomizer::with_seed(42);
assert!(randomizer.bool());
Source

pub fn string(&self, def: StringDef) -> StringDefBuilder<'_>

Create a [StringDefBuilder] based on a given StringDef.

§Example:
use crazy_train::{Randomizer, StringDef};
let string_def = StringDef::default();
let randomizer = Randomizer::with_seed(42);
assert_eq!(randomizer.string(string_def.clone()).to_string(), "noqkak");
assert_eq!(randomizer.string(string_def.clone()).include_capital_letters(true).to_string(), "TWdAyN");
assert_eq!(randomizer.string(string_def.clone()).include_unicode(true).to_string(), "kdnfa😩");
assert_eq!(randomizer.string(string_def.clone()).include_numbers(true).to_string(), "0684n0");
assert_eq!(randomizer.string(string_def.clone()).include_symbol(true).to_string(), "=wqf`g");
assert_eq!(randomizer.string(string_def.clone()).length(10).to_string(), "wgavmyyuzw");
Examples found in repository?
examples/run.rs (line 12)
11    fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
12        let eco_string = randomizer.string(StringDef::default()).to_string();
13
14        Ok(Plan::new::<Self>(format!("echo {eco_string}")))
15    }
16
17    fn is_success(
18        &self,
19        execution_result: &crazy_train::executer::Output,
20        _plan_ctx: &PlanCtx,
21    ) -> Result<bool, &'static str> {
22        if execution_result.status_code == Some(0) {
23            Ok(true)
24        } else {
25            Err("status code should be 0")
26        }
27    }
28
29    fn to_yaml(&self) -> serde_yaml::Value {
30        serde_yaml::to_value(self).expect("serialize")
31    }
32}
33
34#[derive(Serialize, Deserialize)]
35struct StepTwo {}
36
37impl StepTrait for StepTwo {
38    fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
39        let eco_string = randomizer.string(StringDef::default()).to_string();
40
41        Ok(Plan::new::<Self>(format!("unknown-command {eco_string}")))
42    }
Source

pub fn path(&self) -> PathBuf

Generate a random path of a specified length.

§Example:
use crazy_train::Randomizer;
use std::path::PathBuf;
let randomizer = Randomizer::with_seed(42);
assert_eq!(randomizer.path(), PathBuf::from("gowqzkza"));
Source

pub fn shuffle<T>(&self, items: &[T]) -> Vec<T>
where T: Clone,

Shuffle a slice of items and return a new vector with the shuffled items.

§Example:
use crazy_train::Randomizer;
let randomizer = Randomizer::with_seed(42);
let list = vec![1, 2, 3, 4, 5, 6];
assert_eq!(randomizer.shuffle(&list), vec![1, 5, 6, 3, 2, 4]);
Source

pub fn pick_random<T>(&self, items: &[T]) -> Vec<T>
where T: Clone,

Pick a random selection of items from a given slice.

§Example:
use crazy_train::Randomizer;
let randomizer = Randomizer::with_seed(42);
let list = vec![1, 2, 3, 4, 5, 6];
assert_eq!(randomizer.pick_random(&list), vec![2, 6]);

Trait Implementations§

Source§

impl Default for Randomizer

Default implementation for Randomizer, initializing RNG with a random seed.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V