Skip to content

Commit

Permalink
flux: update rand code
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Feb 18, 2025
1 parent 2c4395a commit 71a3c9a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.'wasm32-unknown-unknown']
rustflags = [ "--cfg", "getrandom_backend=\"wasm_js\"" ]
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ glam = "0.29.2"
half = { version = "2", features = ["bytemuck"] }
log = "0.4"
pollster = "0.4"
rand = "0.9"
rand = { version = "0.9", features = ["thread_rng"] }
rand_pcg = "0.9"
rand_seeder = "0.4"
raw-window-handle = "0.6"
Expand Down
1 change: 1 addition & 0 deletions flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
fileSetForCrate = crate: lib.fileset.toSource {
root = ./.;
fileset = lib.fileset.unions [
./.cargo
./Cargo.toml
./Cargo.lock
./flux
Expand Down
12 changes: 6 additions & 6 deletions flux-gl/flux/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// We want to have the option of seeding our RNGs to generate determentistic
// output for testing.

use rand::distributions::{Alphanumeric, Standard};
use rand::distr::{Alphanumeric, StandardUniform};
use rand::prelude::*;
use rand_pcg::Pcg32;
use rand_seeder::Seeder;
Expand All @@ -13,26 +13,26 @@ use std::thread_local;

thread_local!(
static FLUX_RNG: RefCell<Pcg32> = {
let rng = Pcg32::from_rng(thread_rng()).expect("Cannot initialize random number generator");
let rng = Pcg32::from_rng(&mut rand::rng());
RefCell::new(rng)
}
);

pub fn init_from_seed(optional_seed: &Option<String>) {
let seed = optional_seed.as_ref().cloned().unwrap_or_else(|| {
thread_rng()
rand::rng()
.sample_iter(&Alphanumeric)
.take(32)
.map(char::from)
.collect()
});

FLUX_RNG.with(|rng| rng.replace(Seeder::from(seed).make_rng()));
FLUX_RNG.with(|rng| rng.replace(Seeder::from(seed).into_rng()));
}

pub fn gen<T>() -> T
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
FLUX_RNG.with(|rng| rng.borrow_mut().gen::<T>())
FLUX_RNG.with(|rng| rng.borrow_mut().random::<T>())
}
12 changes: 6 additions & 6 deletions flux/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// We want to have the option of seeding our RNGs to generate determentistic
// output for testing.

use rand::distributions::{Alphanumeric, Standard};
use rand::distr::{Alphanumeric, StandardUniform};
use rand::prelude::*;
use rand_pcg::Pcg32;
use rand_seeder::Seeder;
Expand All @@ -13,26 +13,26 @@ use std::thread_local;

thread_local!(
static FLUX_RNG: RefCell<Pcg32> = {
let rng = Pcg32::from_rng(thread_rng()).expect("Cannot initialize random number generator");
let rng = Pcg32::from_rng(&mut rand::rng());
RefCell::new(rng)
}
);

pub fn init_from_seed(optional_seed: &Option<String>) {
let seed = optional_seed.as_ref().cloned().unwrap_or_else(|| {
thread_rng()
rand::rng()
.sample_iter(&Alphanumeric)
.take(32)
.map(char::from)
.collect()
});

FLUX_RNG.with(|rng| rng.replace(Seeder::from(seed).make_rng()));
FLUX_RNG.with(|rng| rng.replace(Seeder::from(seed).into_rng()));
}

pub fn gen<T>() -> T
where
Standard: Distribution<T>,
StandardUniform: Distribution<T>,
{
FLUX_RNG.with(|rng| rng.borrow_mut().gen::<T>())
FLUX_RNG.with(|rng| rng.borrow_mut().random::<T>())
}

0 comments on commit 71a3c9a

Please sign in to comment.