From 49ec2a21a5c88fd051fa95b0b1f9ee49f4b03bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Mauduit?= Date: Wed, 10 Jun 2020 20:50:04 +0200 Subject: [PATCH] Randomize cell initialization --- Cargo.toml | 1 + src/lib.rs | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c4924ca..16bd3a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ default = ["console_error_panic_hook"] [dependencies] wasm-bindgen = "0.2.63" +js-sys = "0.3" # The `console_error_panic_hook` crate provides better debugging of panics by # logging them with `console.error`. This is great for development, but requires diff --git a/src/lib.rs b/src/lib.rs index 898271e..a97a23f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ mod utils; +extern crate js_sys; + use wasm_bindgen::prelude::*; // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global @@ -129,10 +131,9 @@ impl Universe { let width = w; let height = h; - let cells = (0..width * height) - .map(|i| { - if i % 2 == 0 || i % 7 == 0 { + .map(|_| { + if js_sys::Math::random() < 0.5 { Cell::Alive } else { Cell::Dead