Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use faster Rng for generating data #107

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading