Skip to content

Instantly share code, notes, and snippets.

@asterite
Last active February 21, 2025 20:06
Show Gist options
  • Select an option

  • Save asterite/cbea91f23ec600be288bcc39b0a3d14e to your computer and use it in GitHub Desktop.

Select an option

Save asterite/cbea91f23ec600be288bcc39b0a3d14e to your computer and use it in GitHub Desktop.

Revisions

  1. asterite revised this gist Feb 21, 2025. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions config.rs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    use rand::Rng;
    use rand::{Rng, SeedableRng};
    use rand_xorshift::XorShiftRng;

    pub struct Config<T, const N: usize> {
    @@ -33,19 +33,23 @@ impl<T: Copy, const N: usize> Config<T, N> {
    }
    }

    #[derive(Copy, Clone)]
    pub enum Options {
    Addition,
    Subtraction,
    Multiplication,
    Division,
    }

    pub const CONFIG: Config<Options, 4> = Config::new([
    (Options::Addition, 1),
    (Options::Subtraction, 2),
    (Options::Multiplication, 3),
    (Options::Division, 4),
    ]);

    fn main() {}
    #[derive(Copy, Clone, Debug)]
    pub enum Options {
    Addition,
    Subtraction,
    Multiplication,
    Division,
    }

    fn main() {
    let mut rng = XorShiftRng::from_seed([0; 16]);
    let option = CONFIG.select(&mut rng);
    dbg!(option);
    }
  2. asterite revised this gist Feb 21, 2025. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions config.rs
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,6 @@
    use rand::Rng;
    use rand_xorshift::XorShiftRng;

    #[derive(Copy, Clone)]
    pub enum Options {
    Addition,
    Subtraction,
    Multiplication,
    Division,
    }

    pub struct Config<T, const N: usize> {
    pub options_with_weights: [(T, usize); N],
    pub total_weight: usize,
    @@ -41,6 +33,14 @@ impl<T: Copy, const N: usize> Config<T, N> {
    }
    }

    #[derive(Copy, Clone)]
    pub enum Options {
    Addition,
    Subtraction,
    Multiplication,
    Division,
    }

    pub const CONFIG: Config<Options, 4> = Config::new([
    (Options::Addition, 1),
    (Options::Subtraction, 2),
  3. asterite revised this gist Feb 21, 2025. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions config.rs
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,6 @@
    use rand::Rng;
    use rand_xorshift::XorShiftRng;

    pub trait HasWeight {
    fn weight(&self) -> usize;
    }

    #[derive(Copy, Clone)]
    pub enum Options {
    Addition,
  4. asterite created this gist Feb 21, 2025.
    55 changes: 55 additions & 0 deletions config.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    use rand::Rng;
    use rand_xorshift::XorShiftRng;

    pub trait HasWeight {
    fn weight(&self) -> usize;
    }

    #[derive(Copy, Clone)]
    pub enum Options {
    Addition,
    Subtraction,
    Multiplication,
    Division,
    }

    pub struct Config<T, const N: usize> {
    pub options_with_weights: [(T, usize); N],
    pub total_weight: usize,
    }

    impl<T: Copy, const N: usize> Config<T, N> {
    pub const fn new(options_with_weights: [(T, usize); N]) -> Self {
    let mut total_weight = 0;
    let mut i = 0;
    while i < options_with_weights.len() {
    total_weight += options_with_weights[i].1;
    i += 1;
    }

    Self {
    options_with_weights,
    total_weight,
    }
    }

    pub fn select(&self, prng: &mut XorShiftRng) -> T {
    let mut selector = prng.gen_range(0..self.total_weight);
    for (option, weight) in &self.options_with_weights {
    if selector < *weight {
    return *option;
    }
    selector -= weight;
    }
    unreachable!("Should have returned by now")
    }
    }

    pub const CONFIG: Config<Options, 4> = Config::new([
    (Options::Addition, 1),
    (Options::Subtraction, 2),
    (Options::Multiplication, 3),
    (Options::Division, 4),
    ]);

    fn main() {}