summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-14 13:08:08 -0500
committerpommicket <pommicket@gmail.com>2022-12-14 13:08:08 -0500
commit5a20cffba66caa71b495736f75031f69d09ba40b (patch)
tree92834986e8f8c7d1ab8947da52e649c90caac556
parent3265cb676c5c87fd624f59aaf3ca89d947df8cda (diff)
more genrandom
-rw-r--r--gen_random/Cargo.lock45
-rw-r--r--gen_random/src/lib.rs88
-rw-r--r--gen_random_test/src/lib.rs77
-rw-r--r--src/main.rs2
4 files changed, 124 insertions, 88 deletions
diff --git a/gen_random/Cargo.lock b/gen_random/Cargo.lock
index b7e5b20..ad87cc3 100644
--- a/gen_random/Cargo.lock
+++ b/gen_random/Cargo.lock
@@ -12,20 +12,10 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "gen_random"
version = "0.1.0"
dependencies = [
- "gen_random_proc_macro",
"rand",
]
[[package]]
-name = "gen_random_proc_macro"
-version = "0.1.0"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -49,24 +39,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
-name = "proc-macro2"
-version = "1.0.47"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
-dependencies = [
- "unicode-ident",
-]
-
-[[package]]
-name = "quote"
-version = "1.0.21"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
-dependencies = [
- "proc-macro2",
-]
-
-[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -97,23 +69,6 @@ dependencies = [
]
[[package]]
-name = "syn"
-version = "1.0.105"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
-dependencies = [
- "proc-macro2",
- "quote",
- "unicode-ident",
-]
-
-[[package]]
-name = "unicode-ident"
-version = "1.0.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
-
-[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/gen_random/src/lib.rs b/gen_random/src/lib.rs
index f091ec3..b9cdb4f 100644
--- a/gen_random/src/lib.rs
+++ b/gen_random/src/lib.rs
@@ -1,5 +1,8 @@
extern crate rand;
use rand::Rng;
+use std::rc::Rc;
+use std::sync::Arc;
+use std::cell::{Cell, RefCell};
pub trait GenRandom: Sized {
fn gen_random(rng: &mut impl Rng) -> Self;
@@ -9,6 +12,14 @@ pub trait GenRandom: Sized {
}
}
+pub fn gen_random_vec<T: GenRandom>(rng: &mut impl Rng, len: usize) -> Vec<T> {
+ (0..len).map(|_| T::gen_random(rng)).collect()
+}
+
+pub fn gen_thread_random_vec<T: GenRandom>(len: usize) -> Vec<T> {
+ gen_random_vec(&mut rand::thread_rng(), len)
+}
+
impl GenRandom for f32 {
fn gen_random(rng: &mut impl Rng) -> Self {
rng.gen_range(0.0..1.0)
@@ -26,3 +37,80 @@ impl<T: GenRandom> GenRandom for Box<T> {
Box::new(T::gen_random(rng))
}
}
+
+impl<T: GenRandom> GenRandom for [T; 1] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for [T; 2] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng), T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for [T; 3] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for [T; 4] {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ [T::gen_random(rng), T::gen_random(rng), T::gen_random(rng), T::gen_random(rng)]
+ }
+}
+
+impl<T: GenRandom> GenRandom for (T, T) {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ (T::gen_random(rng), T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for (T, T, T) {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ (T::gen_random(rng), T::gen_random(rng), T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for (T, T, T, T) {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ (T::gen_random(rng), T::gen_random(rng), T::gen_random(rng), T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Rc<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Arc<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Cell<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for RefCell<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Self::new(T::gen_random(rng))
+ }
+}
+
+impl<T: GenRandom> GenRandom for Option<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ if rng.gen_range(0..2) == 0 {
+ None
+ } else {
+ Some(T::gen_random(rng))
+ }
+ }
+}
+
diff --git a/gen_random_test/src/lib.rs b/gen_random_test/src/lib.rs
index faca129..00f5026 100644
--- a/gen_random_test/src/lib.rs
+++ b/gen_random_test/src/lib.rs
@@ -1,57 +1,48 @@
-extern crate rand;
-extern crate gen_random_proc_macro;
-extern crate gen_random;
-use gen_random::GenRandom;
-use gen_random_proc_macro::GenRandom;
-
-#[derive(GenRandom, Debug)]
-enum Test1 {
- #[prob = 0.2]
- A(f32),
- #[prob = 0.8]
- B(f32)
-}
-
-#[derive(GenRandom, Debug)]
-#[allow(dead_code)]
-enum Test2 {
- #[prob = 0.1]
- Variant1,
- #[prob = 0.7]
- Variant2 { x : f32, y: f64, z: Test1 },
- #[prob = 0.2]
- Variant3(f32, Box<Test2>)
-}
-
-#[derive(GenRandom, Debug)]
-enum LinkedList {
- #[prob = 0.1]
- Empty,
- #[prob = 0.9]
- Cons(f32, Box<LinkedList>)
-}
-
#[cfg(test)]
mod tests {
- use super::*;
+ extern crate rand;
+ extern crate gen_random_proc_macro;
+ extern crate gen_random;
+ use gen_random::{GenRandom, gen_thread_random_vec};
+ use gen_random_proc_macro::GenRandom;
+
+ #[derive(GenRandom, Debug)]
+ enum Test1 {
+ #[prob = 0.2]
+ A(f32),
+ #[prob = 0.8]
+ B(Option<f32>)
+ }
+
+ #[derive(GenRandom, Debug)]
+ #[allow(dead_code)]
+ enum Test2 {
+ #[prob = 0.1]
+ Variant1,
+ #[prob = 0.7]
+ Variant2 { x : f32, y: f64, z: Test1 },
+ #[prob = 0.2]
+ Variant3(f32, Box<Test2>)
+ }
+
+ #[derive(GenRandom, Debug)]
+ enum LinkedList {
+ #[prob = 0.1]
+ Empty,
+ #[prob = 0.9]
+ Cons(f32, Box<LinkedList>)
+ }
#[test]
fn basic() {
- let mut rng = rand::thread_rng();
-
- let tests1: Vec<_> = (0..10).map(|_| {
- Test1::gen_random(&mut rng)
- }).collect();
+ let tests1: Vec<Test1> = gen_thread_random_vec(10);
println!("{tests1:?}");
}
#[test]
fn many_types_of_variants() {
- let mut rng = rand::thread_rng();
- let tests2: Vec<_> = (0..10).map(|_| {
- Test2::gen_random(&mut rng)
- }).collect();
+ let tests2: Vec<Test2> = gen_thread_random_vec(10);
println!("{tests2:?}");
}
diff --git a/src/main.rs b/src/main.rs
index 60e2499..ab2aa40 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,7 @@
/*
@TODO:
+- use 0..(sum of probs) for variant
+- scale and bias
- fullscreen key
- mathematical analysis
- options for: