random-constructible
random-constructible is a Rust crate that provides traits and macros to facilitate the random generation of primitive types and enums, with support for custom probability distributions. It simplifies the process of creating random instances of your types, especially when dealing with enums that require weighted random selection.
Features
- Random Generation for Primitive Types: Automatically implements random generation for all primitive integer and floating-point types.
- Random Enums with Custom Probabilities: Easily define how your enums should be randomly generated, including specifying custom weights for each variant.
- Uniform Random Generation: Support for uniform random generation across all variants.
- Extensible Probability Maps: Create and use custom probability maps for more complex random generation scenarios.
Installation
Add the following to your Cargo.toml:
[]
= "0.6.0"
and include it in your crate:
use *;
Getting Started
Random Generation for Primitive Types
The crate automatically provides implementations for all primitive integer and floating-point types. You can generate random values using:
use RandConstruct;
let random_u32: u32 = u32random;
let random_f64: f64 = f64random;
Random Enums
To enable random generation for your enums, implement the RandConstructEnum trait. You can do this manually or use the provided macros for convenience.
Manual Implementation
use ;
Using the rand_construct_env! Macro
Alternatively, you can use the rand_construct_env! macro to define the probability map:
use ;
;
rand_construct_env!;
Sampling Random Variants
Once you've implemented RandConstructEnum for your enum, you can generate random variants:
use RandConstruct;
let random_variant = random; // Uses default weights
let uniform_variant = uniform; // Uniform probability
To sample using a custom provider:
use RandConstructEnumWithEnv;
let random_variant = ;
Examples
Complete Example
use ;
use Rng;
// Define your enum
// Implement RandConstructEnum
// Define a custom probability provider
;
rand_construct_env!;
Testing Randomness
The crate also provides utilities for testing the distribution of your random generation:
use ;
use HashMap;
License
This project is licensed under the MIT License - see the LICENSE file for details.
Note: This README is generated based on the crate's code and is meant to help you get started with random-constructible. For more detailed information, please refer to the documentation.