Skip to content

Commit cc38918

Browse files
committed
Remember to return the generated value, not the upper bound :P
1 parent 3d4340c commit cc38918

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/algorithm/dyadic/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,9 +2173,9 @@ impl Value {
21732173
let i = RNG.with_borrow_mut(|rng| {
21742174
let upper = len.next_power_of_two();
21752175
loop {
2176-
let r = rng.next_u64() as usize;
2177-
if r % upper < len {
2178-
break len;
2176+
let r = rng.next_u64() as usize % upper;
2177+
if r < len {
2178+
break r;
21792179
}
21802180
}
21812181
});

src/algorithm/monadic/sort.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,15 @@ impl<T: ArrayValue> Array<T> {
233233
let row_len = self.row_len();
234234
let slice = self.data.as_mut_slice();
235235
for i in (1..row_count).rev() {
236-
let j = rng.gen_range(0..=i);
236+
let j = {
237+
let upper = i.next_power_of_two();
238+
loop {
239+
let r = rng.next_u64() as usize % upper;
240+
if r <= i {
241+
break r;
242+
}
243+
}
244+
};
237245
if i == j {
238246
continue;
239247
}

src/run_prim.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,8 @@ fn undo_regex(env: &mut Uiua) -> UiuaResult {
17741774
}
17751775

17761776
thread_local! {
1777-
pub(crate) static RNG: RefCell<Xoshiro256Plus> = RefCell::new(Xoshiro256Plus::from_seed(transmute(SystemTime::now().saturating_duration_since(UNIX_EPOCH).as_micros())));
1777+
pub(crate) static RNG: RefCell<Xoshiro256Plus> = RefCell::new(Xoshiro256Plus::seed_from_u64(
1778+
SystemTime::now().duration_since(UNIX_EPOCH).map(|t|t.as_micros()).unwrap_or(42) as u64));
17781779
}
17791780

17801781
/// Generate a random number, equivalent to [`Primitive::Rand`]

0 commit comments

Comments
 (0)