GithubHelp home page GithubHelp logo

choose-rand's Introduction

choose-rand

A small crate for choosing random items from a set of weighted items.

Examples

use choose_rand::prelude::*;
 
 #[derive(Debug, Clone)]
 struct Foo {
     prob: f32,
 }
 
 impl Probable for Foo {
     fn probability(&self) -> f32 {
         self.prob
     }
 }
 
 fn main() -> Result<()> {
     let v: Vec<_> = choose_rand::helper::refcellify(
         vec![Foo { prob: 0.25 }, Foo { prob: 0.5 }, Foo { prob: 0.1 }, Foo { prob: 0.05 }]
     ).collect();
 
     let mut rng = rand::thread_rng();    
     dbg!(v.choose_rand(&mut rng)?);
 
     Ok(())
 }

with features = ["eq_float"] for sets:

use choose_rand::prelude::*;
use eq_float::F32;
use std::collections::HashSet;

#[derive(Clone, Debug, Hash)]
struct Foo {
    prob: F32,
}

impl Foo {
    fn new(prob: f32) -> Self {
        Self { prob: F32(prob) }
    }
}

impl Probable for Foo {
    fn probablity(&self) -> F32 {
        self.prob
    }
}

fn main() -> Result<()> {
     let s: HashSet<_> = choose_rand::helper::refcellify(
         vec![Foo::new(0.25), Foo::new(0.5), Foo::new(0.1), Foo::new(0.05)]
     ).collect();
 
     let mut rng = rand::thread_rng();    
     dbg!(s.choose_rand(&mut rng)?);
 
     Ok(())
 }

License

This library is licensed under the MIT license.

choose-rand's People

Contributors

hypercodec avatar

Watchers

 avatar

choose-rand's Issues

Crate validity?

This crate isn't super useful, as it depends on another crate that can easily do the same thing. All this crate really does is provide some blanket impls and pretty bad practices.

Useless helper mod.

The module isnt really useful, as it only has one function and the code saved by it doesn't quantify its existence

remove clones

this is just a shitty repo and no one probably cares but the code is awful so i should rewrite it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.