summaryrefslogtreecommitdiff
path: root/src/hitomezashi.rs
diff options
context:
space:
mode:
authorNicholas Johnson <nick@nicholasjohnson.ch>2024-04-19 00:00:00 +0000
committerNicholas Johnson <nick@nicholasjohnson.ch>2024-04-19 00:00:00 +0000
commitf6130ec71273f22b6d9b9b4c17811cec83052eab74e8ad50b394b92fb63e9062 (patch)
tree59fac352683f2474276340689b1c7b0a65b26b108e8c419d795de43f54cca012 /src/hitomezashi.rs
parent202d02b86998c700ae799e961550d702df6b8a2b62541dd9cff9a9e0a091c2df (diff)
downloadhitomezashi-rs-f6130ec71273f22b6d9b9b4c17811cec83052eab74e8ad50b394b92fb63e9062.tar.gz
hitomezashi-rs-f6130ec71273f22b6d9b9b4c17811cec83052eab74e8ad50b394b92fb63e9062.zip
Sample random booleans more efficiently
Diffstat (limited to 'src/hitomezashi.rs')
-rw-r--r--src/hitomezashi.rs7
1 files changed, 4 insertions, 3 deletions
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 <https://www.gnu.org/licenses/>.
*/
-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<f32>) {
};
let mut rng = rand::thread_rng();
+ let brn = Bernoulli::new(skew).unwrap();
let init_bit: bool = rng.gen::<bool>();
let mut row_bits: Vec<bool> = Vec::with_capacity(height - 1);
let mut col_bits: Vec<bool> = 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