Crates.io | rand-construct |
lib.rs | rand-construct |
version | 0.10.0 |
source | src |
created_at | 2024-11-17 17:00:54.643986 |
updated_at | 2024-11-22 02:17:02.956554 |
description | Encapsulates the random-constructible and random-constructible-derive crates which are used for creating random instances of data structures with weighted probabilities |
homepage | https://github.com/klebs6/klebs-general |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1451406 |
size | 9,373 |
rand-construct
is a Rust crate that provides a derive macro to automatically implement traits for generating random instances of your types. It allows you to easily generate random enums and structs, with customizable probabilities for enum variants, and supports custom environments to influence randomness.
RandConstruct
for your enums and structs.rand
crate for randomness.Add the following to your Cargo.toml
:
[dependencies]
rand-construct = "0.6.0" # Replace with the latest version
In your Rust file, import the necessary traits and macros:
use rand_construct::{RandConstruct, RandConstructEnvironment};
RandConstruct
for EnumsYou can automatically implement random construction for your enums by using the #[derive(RandConstruct)]
macro. You can also specify default unnormalized probabilities for each variant using the #[rand_construct(p = value)]
attribute.
use rand_construct::RandConstruct;
#[derive(RandConstruct, Debug)]
enum Weather {
#[rand_construct(p = 5.0)]
Sunny,
#[rand_construct(p = 2.0)]
Rainy,
#[rand_construct(p = 1.0)]
Cloudy,
#[rand_construct(p = 0.5)]
Snowy,
}
fn main() {
let random_weather = Weather::random();
println!("Random Weather: {:?}", random_weather);
}
#[derive(RandConstruct)]
: Automatically implements random construction for Weather
.#[rand_construct(p = value)]
: Sets the default weight for each variant.Weather::random()
: Generates a random instance of Weather
based on the specified probabilities.RandConstruct
for StructsFor structs, you can derive RandConstruct
, and the macro will automatically implement the trait by generating random instances of each field.
use rand_construct::RandConstruct;
#[derive(RandConstruct, Debug)]
struct Dimensions {
width: u32,
height: u32,
}
fn main() {
let random_dimensions = Dimensions::random();
println!("Random Dimensions: {:?}", random_dimensions);
}
RandConstruct
.u32
, RandConstruct
is implemented by default.You can define custom environments to alter the probabilities of random generation. This is useful when you want different contexts to influence the randomness.
First, derive RandConstructEnvironment
for your environment struct:
use rand_construct::RandConstructEnvironment;
#[derive(RandConstructEnvironment)]
struct DesertEnvironment;
Then, use the rand_construct_env!
macro to define custom probabilities for your enums within this environment:
use rand_construct::{rand_construct_env, RandConstruct};
#[derive(RandConstruct, Debug)]
enum Animal {
#[rand_construct(p = 1.0)]
Camel,
#[rand_construct(p = 1.0)]
Scorpion,
#[rand_construct(p = 1.0)]
FennecFox,
}
rand_construct_env! {
DesertEnvironment => Animal {
Camel => 5.0,
Scorpion => 3.0,
FennecFox => 2.0,
}
}
You can now generate random instances using the custom environment:
fn main() {
let desert_animal = Animal::random_with_env::<DesertEnvironment>();
println!("Desert Animal: {:?}", desert_animal);
}
#[derive(RandConstructEnvironment)]
: Marks DesertEnvironment
as a custom environment.rand_construct_env!
: Defines custom probabilities for Animal
variants within DesertEnvironment
.Animal::random_with_env::<DesertEnvironment>()
: Generates a random Animal
using the probabilities defined in DesertEnvironment
.use rand_construct::{RandConstruct, RandConstructEnvironment, rand_construct_env};
#[derive(RandConstruct, Debug)]
enum Terrain {
#[rand_construct(p = 1.0)]
Sand,
#[rand_construct(p = 1.0)]
Rock,
#[rand_construct(p = 1.0)]
Oasis,
}
rand_construct_env! {
DesertEnvironment => Terrain {
Sand => 8.0,
Rock => 2.0,
Oasis => 1.0,
}
}
fn main() {
let random_terrain = Terrain::random_with_env::<DesertEnvironment>();
println!("Random Terrain in Desert: {:?}", random_terrain);
}
Random Terrain in Desert: Sand
#[rand_construct(p = value)]
1.0
.#[derive(RandConstruct, Debug)]
enum Beverage {
#[rand_construct(p = 3.0)]
Water,
#[rand_construct(p = 1.0)]
Soda,
#[rand_construct(p = 0.5)]
Juice,
Coffee, // Default weight is 1.0
}
You can generate random instances with uniform probabilities using the uniform
method:
use rand_construct::RandConstruct;
fn main() {
let uniform_weather = Weather::uniform();
println!("Uniform Weather: {:?}", uniform_weather);
}
You can generate random instances using a custom random number generator:
use rand::rngs::StdRng;
use rand::SeedableRng;
use rand_construct::RandConstruct;
fn main() {
let mut rng = StdRng::seed_from_u64(42);
let random_weather = Weather::random_with_rng(&mut rng);
println!("Random Weather with Custom RNG: {:?}", random_weather);
}
RandConstruct
.rand
Craterand-construct
integrates seamlessly with the rand
crate. It uses rand
under the hood to generate random values.
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.