Skip to content

Commit

Permalink
Removing rand crate dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gents83 committed Feb 1, 2025
1 parent 432d493 commit 4a98279
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
2 changes: 0 additions & 2 deletions crates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 1 addition & 3 deletions crates/math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ license.workspace = true
crate-type = ["lib"]

[dependencies]
cgmath = { workspace = true }
getrandom = { workspace = true, optional = true}
rand = { workspace = true }
cgmath = { workspace = true }
18 changes: 15 additions & 3 deletions crates/math/src/random.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4a98279

Please sign in to comment.