random-constructible
random-constructible is a Rust crate that provides traits and utilities to easily generate random instances of enums with customizable probabilities. It allows you to:
- Define enums that can be randomly instantiated.
- Assign default weights to enum variants.
- Use custom probability maps to influence the randomness.
- Generate random instances uniformly or based on specified probabilities.
Features
- RandConstruct Trait: A trait that provides methods to generate random instances.
- RandConstructEnum Trait: Extends
RandConstructfor enums, allowing for default weights and custom probability maps. - RandConstructProbabilityMapProvider Trait: Allows for custom probability maps to be provided.
- Macro for Probability Map Providers: A convenient macro to define custom probability map providers.
- Support for Custom Environments: Use
RandConstructEnvironmentto define environments with specific probability maps.
Getting Started
Add Dependency
Add the following to your Cargo.toml:
[]
= "0.1.0"
Derive RandConstructEnum for Your Enum
First, define your enum and implement RandConstructEnum for it. You'll need to provide:
- A
default_weightfor each variant. - A list of all variants.
- A default probability map (usually via a provider).
use ;
use HashMap;
use Arc;
Define a Probability Map Provider
Use the rand_construct_env! macro to define a provider for your enum:
use rand_construct_env;
;
rand_construct_env!;
Generate Random Instances
Now you can generate random instances of your enum:
Use Custom Probability Maps
You can define custom providers to alter the probabilities:
;
rand_construct_env!;
Traits and Macros
RandConstruct Trait
Provides basic methods to generate random instances:
fn random() -> Self: Generates a random instance based on default probabilities.fn uniform() -> Self: Generates a random instance with uniform probability.
RandConstructEnum Trait
Extends RandConstruct for enums:
fn default_weight(&self) -> f64: Returns the default weight of a variant.fn all_variants() -> Vec<Self>: Returns all variants of the enum.fn create_default_probability_map() -> Arc<HashMap<Self, f64>>: Creates the default probability map.- Additional methods to sample with custom probabilities and providers.
RandConstructProbabilityMapProvider Trait
Allows custom probability maps:
fn probability_map() -> Arc<HashMap<R, f64>>: Returns the custom probability map.fn uniform_probability_map() -> Arc<HashMap<R, f64>>: Returns a uniform probability map.
rand_construct_env! Macro
Simplifies the creation of probability map providers:
rand_construct_env!;
Examples
Full Example
use ;
use HashMap;
use Arc;
;
rand_construct_env!;
Using a Custom Environment
use ;
use HashMap;
use Arc;
;
rand_construct_env!;
Testing
The crate includes a comprehensive set of tests to ensure correctness. The tests cover:
- Validation of all variants.
- Correct default weights.
- Random generation based on default probabilities.
- Uniform random generation.
- Random generation using custom probability maps.
- Sampling using custom providers.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contribution
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Acknowledgments
- Inspired by the need for customizable random generation in Rust enums.
- Utilizes the
randcrate for randomness andonce_cellfor lazy static initialization.
Contact
For questions or suggestions, feel free to open an issue or contact the maintainer.