From 4a982799a0a58f800d116c5bdfbfb4f53da48ccf Mon Sep 17 00:00:00 2001 From: gents83 Date: Sat, 1 Feb 2025 13:25:02 +0100 Subject: [PATCH] Removing rand crate dependency --- .github/workflows/rust.yml | 4 ++-- crates/Cargo.toml | 2 -- crates/math/Cargo.toml | 4 +--- crates/math/src/random.rs | 18 +++++++++++++++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 254c0b4a..b13a997b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,8 +13,8 @@ on: env: CARGO_TERM_COLOR: always CARGO_TARGET_DIR: ./crates/target - RUSTFLAGS: '--cfg getrandom_backend="wasm_js" --cfg=web_sys_unstable_apis' # required for wasmbindgen to work - + RUSTFLAGS: '--cfg=web_sys_unstable_apis' # required for webgpu + jobs: check: name: "Build on OS: ${{ matrix.os }}" diff --git a/crates/Cargo.toml b/crates/Cargo.toml index 67a7ee40..1c284675 100644 --- a/crates/Cargo.toml +++ b/crates/Cargo.toml @@ -45,8 +45,6 @@ downcast-rs = { git = "https://github.com/marcianx/downcast-rs" } egui = { git = "https://github.com/emilk/egui" } image = { git = "https://github.com/image-rs/image", version = "0.25.0", default-features = false, features = [ "default-formats"] } pyo3 = { git = "https://github.com/PyO3/pyo3", features = ["macros", "extension-module", "auto-initialize", "abi3-py310", "generate-import-lib"] } -getrandom = { version = "0.3", features = ["wasm_js"] } -rand = { git = "https://github.com/rust-random/rand", features = ["std"] } serde_json = { git = "https://github.com/serde-rs/json"} flexbuffers = { version = "2.0.0"} ttf-parser = { git = "https://github.com/RazrFalcon/ttf-parser" } diff --git a/crates/math/Cargo.toml b/crates/math/Cargo.toml index d76dd86b..c0c4caf1 100644 --- a/crates/math/Cargo.toml +++ b/crates/math/Cargo.toml @@ -12,6 +12,4 @@ license.workspace = true crate-type = ["lib"] [dependencies] -cgmath = { workspace = true } -getrandom = { workspace = true, optional = true} -rand = { workspace = true } \ No newline at end of file +cgmath = { workspace = true } \ No newline at end of file diff --git a/crates/math/src/random.rs b/crates/math/src/random.rs index 84c4233e..044a5297 100644 --- a/crates/math/src/random.rs +++ b/crates/math/src/random.rs @@ -1,8 +1,20 @@ -use rand::Rng; +use std::time::{SystemTime, UNIX_EPOCH}; pub fn get_random_f32(min: f32, max: f32) -> f32 { - rand::rng().random_range(min..max) + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + + let nanos = now.as_nanos() as u64; // Get nanoseconds as u64 + let normalized = (nanos % 1_000_000_000) as f32 / 1_000_000_000.0; // Normalize to [0,1) + + min + normalized * (max - min) // Scale to range [min, max] } pub fn get_random_u32(min: u32, max: u32) -> u32 { - rand::rng().random_range(min..max) + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + + let nanos = now.as_nanos() as u64; // Get nanoseconds as u64 + (nanos % ((max - min + 1) as u64)) as u32 + min }