Skip to content

Commit 64e56a5

Browse files
committed
Implement gen_range(-1..=1), seems to be more uniform than the previous way
1 parent cc38918 commit 64e56a5

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/algorithm/monadic/sort.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::{cmp::Ordering, ptr};
22

33
use ecow::EcoVec;
4-
use rand_xoshiro::rand_core::SeedableRng;
5-
use rand_xoshiro::Xoshiro256Plus;
4+
use rand_xoshiro::{
5+
rand_core::{RngCore, SeedableRng},
6+
Xoshiro256Plus,
7+
};
68
use rayon::prelude::*;
79

810
use crate::{algorithm::ArrayCmpSlice, random_with, val_as_arr, Array, ArrayValue, Value};
@@ -242,6 +244,7 @@ impl<T: ArrayValue> Array<T> {
242244
}
243245
}
244246
};
247+
245248
if i == j {
246249
continue;
247250
}

src/constant.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use std::{
55
};
66

77
use ecow::EcoVec;
8-
use rand_xoshiro::rand_core::SeedableRng;
9-
use rand_xoshiro::Xoshiro256Plus;
8+
use rand_xoshiro::{
9+
rand_core::{RngCore, SeedableRng},
10+
Xoshiro256Plus,
11+
};
1012

1113
use crate::{
1214
parse_doc_line_fragments, Array, Boxed, PrimDocFragment, SysBackend, Value, WILDCARD_NAN,
@@ -465,6 +467,8 @@ fn music_constant(backend: &dyn SysBackend) -> Value {
465467
hat_bits >>= 1;
466468
}
467469
let mut rng = Xoshiro256Plus::seed_from_u64(0);
470+
let mut rand_wrench =
471+
|| 2.0 * f64::from_bits(rng.next_u64() >> 12 | 0x3FF0_0000_0000_0000) - 3.0; // gen_range(-1..=1)
468472
let sr = backend.audio_sample_rate();
469473
(0..(BEAT * 2.0 * 16.0 * sr as f64) as usize)
470474
.map(|s| {
@@ -478,11 +482,11 @@ fn music_constant(backend: &dyn SysBackend) -> Value {
478482
let h = if (h * secs % 1.0) < 0.5 { 1.0 } else { -1.0 } / 3.0; // Square wave
479483
let kick = ((secs % BEAT).powf(0.4) * 40.0 * TAU).sin();
480484
let hat = 0.3
481-
* rng.gen_range(-1.0..=1.0)
485+
* rand_wrench()
482486
* hat_mask[(4.0 * beat) as usize % 32]
483487
* (0.0..=0.1).contains(&(secs % (BEAT / 4.0) / (BEAT / 4.0))) as u8 as f64;
484488
let snare = 0.5
485-
* rng.gen_range(-1.0..=1.0)
489+
* rand_wrench()
486490
* ((0.5..=0.6).contains(&(secs % (2.0 * BEAT) / (2.0 * BEAT))) as u8 as f64);
487491

488492
0.5 * (m + h + kick + hat + snare)

0 commit comments

Comments
 (0)