From f6130ec71273f22b6d9b9b4c17811cec83052eab74e8ad50b394b92fb63e9062 Mon Sep 17 00:00:00 2001 From: Nicholas Johnson Date: Fri, 19 Apr 2024 00:00:00 +0000 Subject: Sample random booleans more efficiently --- src/hitomezashi.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/hitomezashi.rs') diff --git a/src/hitomezashi.rs b/src/hitomezashi.rs index da6e296..d5aa417 100644 --- a/src/hitomezashi.rs +++ b/src/hitomezashi.rs @@ -16,7 +16,7 @@ along with this program. If not, see . */ -use rand::Rng; +use rand::distributions::{Bernoulli, Distribution}; const TRANSPARENT_SQUARE: char = ' '; const OPAQUE_SQUARE: char = '█'; @@ -32,17 +32,18 @@ pub fn hitomezashi(width: usize, height: usize, skew: Option) { }; let mut rng = rand::thread_rng(); + let brn = Bernoulli::new(skew).unwrap(); let init_bit: bool = rng.gen::(); let mut row_bits: Vec = Vec::with_capacity(height - 1); let mut col_bits: Vec = Vec::with_capacity(width - 1); for _ in 0..(height - 1) { - row_bits.push(rng.gen_bool(skew)); + row_bits.push(brn.sample(&mut rng)); } for _ in 0..(width - 1) { - col_bits.push(rng.gen_bool(skew)); + col_bits.push(brn.sample(&mut rng)); } // each new row of the pattern depends on the bits directly above it -- cgit v1.2.3