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: u64Implementations§
Source§impl Randomizer
impl Randomizer
Sourcepub fn with_seed(seed: u64) -> Self
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);Sourcepub fn number_between(&self, min: u32, max: u32) -> u32
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);Sourcepub fn bool(&self) -> bool
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());Sourcepub fn string(&self, def: StringDef) -> StringDefBuilder<'_>
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 }Sourcepub fn path(&self) -> PathBuf
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"));Sourcepub fn shuffle<T>(&self, items: &[T]) -> Vec<T>where
T: Clone,
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]);Trait Implementations§
Source§impl Default for Randomizer
Default implementation for Randomizer, initializing RNG with a random seed.
impl Default for Randomizer
Default implementation for Randomizer, initializing RNG with a random seed.
Auto Trait Implementations§
impl !Freeze for Randomizer
impl !RefUnwindSafe for Randomizer
impl Send for Randomizer
impl !Sync for Randomizer
impl Unpin for Randomizer
impl !UnwindSafe for Randomizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more