crazy_train

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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
        let eco_string = randomizer.string(StringDef::default()).to_string();
        Ok(Plan {
            id: std::any::type_name::<Self>().to_string(),
            command: format!("echo {eco_string}"),
        })
    }

    fn is_success(
        &self,
        execution_result: &crazy_train::executer::Output,
    ) -> Result<bool, &'static str> {
        if execution_result.status_code == Some(0) {
            Ok(true)
        } else {
            Err("status code should be 0")
        }
    }

    fn to_yaml(&self) -> serde_yaml::Value {
        serde_yaml::to_value(self).expect("serialize")
    }
}

#[derive(Serialize, Deserialize)]
struct StepTwo {}

impl StepTrait for StepTwo {
    fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result<crazy_train::step::Plan> {
        let eco_string = randomizer.string(StringDef::default()).to_string();
        Ok(Plan {
            id: std::any::type_name::<Self>().to_string(),
            command: format!("unknown-command {eco_string}"),
        })
    }
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