Skip to content

Commit c78633a

Browse files
amatgilkaikalii
authored andcommitted
Remember to return the generated value, not the upper bound :P
1 parent fe5b433 commit c78633a

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
@@ -2175,9 +2175,9 @@ impl Value {
21752175
let i = RNG.with_borrow_mut(|rng| {
21762176
let upper = len.next_power_of_two();
21772177
loop {
2178-
let r = rng.next_u64() as usize;
2179-
if r % upper < len {
2180-
break len;
2178+
let r = rng.next_u64() as usize % upper;
2179+
if r < len {
2180+
break r;
21812181
}
21822182
}
21832183
});

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
@@ -1788,7 +1788,8 @@ fn undo_regex(env: &mut Uiua) -> UiuaResult {
17881788
}
17891789

17901790
thread_local! {
1791-
pub(crate) static RNG: RefCell<Xoshiro256Plus> = RefCell::new(Xoshiro256Plus::from_seed(transmute(SystemTime::now().saturating_duration_since(UNIX_EPOCH).as_micros())));
1791+
pub(crate) static RNG: RefCell<Xoshiro256Plus> = RefCell::new(Xoshiro256Plus::seed_from_u64(
1792+
SystemTime::now().duration_since(UNIX_EPOCH).map(|t|t.as_micros()).unwrap_or(42) as u64));
17921793
}
17931794

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

0 commit comments

Comments
 (0)