Skip to content

Commit

Permalink
Use faster Rng for generating data
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolaczk committed Aug 12, 2024
1 parent 5154216 commit 031d976
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/scripting/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::scripting::Resources;
use chrono::Utc;
use metrohash::MetroHash64;
use rand::distributions::Distribution;
use rand::prelude::StdRng;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use rune::macros::{quote, MacroContext, TokenStream};
use rune::parse::Parser;
Expand Down Expand Up @@ -91,15 +91,15 @@ pub fn hash_range(i: i64, max: i64) -> i64 {
/// Generates a floating point value with normal distribution
#[rune::function]
pub fn normal(i: i64, mean: f64, std_dev: f64) -> VmResult<f64> {
let mut rng = StdRng::seed_from_u64(i as u64);
let mut rng = SmallRng::seed_from_u64(i as u64);
let distribution =
vm_try!(Normal::new(mean, std_dev).map_err(|e| VmError::panic(format!("{e}"))));
VmResult::Ok(distribution.sample(&mut rng))
}

#[rune::function]
pub fn uniform(i: i64, min: f64, max: f64) -> VmResult<f64> {
let mut rng = StdRng::seed_from_u64(i as u64);
let mut rng = SmallRng::seed_from_u64(i as u64);
let distribution = vm_try!(Uniform::new(min, max).map_err(|e| VmError::panic(format!("{e}"))));
VmResult::Ok(distribution.sample(&mut rng))
}
Expand All @@ -108,7 +108,7 @@ pub fn uniform(i: i64, min: f64, max: f64) -> VmResult<f64> {
/// Parameter `seed` is used to seed the RNG.
#[rune::function]
pub fn blob(seed: i64, len: usize) -> Vec<u8> {
let mut rng = StdRng::seed_from_u64(seed as u64);
let mut rng = SmallRng::seed_from_u64(seed as u64);
(0..len).map(|_| rng.gen::<u8>()).collect()
}

Expand All @@ -117,7 +117,7 @@ pub fn blob(seed: i64, len: usize) -> Vec<u8> {
/// the RNG.
#[rune::function]
pub fn text(seed: i64, len: usize) -> String {
let mut rng = StdRng::seed_from_u64(seed as u64);
let mut rng = SmallRng::seed_from_u64(seed as u64);
(0..len)
.map(|_| {
let code_point = rng.gen_range(0x0061u32..=0x007Au32); // Unicode range for 'a-z'
Expand Down

0 comments on commit 031d976

Please sign in to comment.