-
-
Couldn't load subscription status.
- Fork 150
Make gen portable
#841
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
base: main
Are you sure you want to change the base?
Make gen portable
#841
Changes from all commits
fe5b433
c78633a
1a8ab14
c18f4f7
0015f2b
6a20535
5f75eaa
5bc84dd
132b4b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,14 +8,19 @@ use core::f64; | |||||||||||||||||
| use std::{ | ||||||||||||||||||
| borrow::Cow, | ||||||||||||||||||
| cmp::Ordering, | ||||||||||||||||||
| hash::{DefaultHasher, Hash, Hasher}, | ||||||||||||||||||
| hash::{Hash, Hasher}, | ||||||||||||||||||
| iter::{once, repeat_n}, | ||||||||||||||||||
| mem::{replace, swap, take}, | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| use ahash::AHasher; | ||||||||||||||||||
| use bytemuck::allocation::cast_vec; | ||||||||||||||||||
| use ecow::{eco_vec, EcoVec}; | ||||||||||||||||||
| use rand::prelude::*; | ||||||||||||||||||
| use rand_xoshiro::{ | ||||||||||||||||||
| rand_core::{RngCore, SeedableRng}, | ||||||||||||||||||
| Xoshiro256Plus, | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| #[cfg(not(target_arch = "wasm32"))] | ||||||||||||||||||
| use rayon::prelude::*; | ||||||||||||||||||
| use smallvec::SmallVec; | ||||||||||||||||||
|
|
@@ -2109,10 +2114,10 @@ impl Value { | |||||||||||||||||
| } | ||||||||||||||||||
| /// Generate randomly seeded arrays | ||||||||||||||||||
| pub fn gen(&self, seed: &Self, env: &Uiua) -> UiuaResult<Value> { | ||||||||||||||||||
| let mut hasher = DefaultHasher::new(); | ||||||||||||||||||
| let mut hasher = AHasher::default(); | ||||||||||||||||||
| seed.hash(&mut hasher); | ||||||||||||||||||
| let seed = hasher.finish(); | ||||||||||||||||||
| let mut rng = SmallRng::seed_from_u64(seed); | ||||||||||||||||||
| let mut rng = Xoshiro256Plus::seed_from_u64(seed); | ||||||||||||||||||
|
|
||||||||||||||||||
| const SHAPE_REQ: &str = "Shape must be an array of natural \ | ||||||||||||||||||
| numbers with at most rank 2"; | ||||||||||||||||||
|
|
@@ -2122,7 +2127,7 @@ impl Value { | |||||||||||||||||
| let elem_count = validate_size::<f64>(shape.iter().copied(), env)?; | ||||||||||||||||||
| let mut data = eco_vec![0.0; elem_count]; | ||||||||||||||||||
| for x in data.make_mut() { | ||||||||||||||||||
| *x = rng.gen(); | ||||||||||||||||||
| *x = f64::from_bits(rng.next_u64() >> 12 | 0x3FF0_0000_0000_0000) - 1.0; | ||||||||||||||||||
| } | ||||||||||||||||||
| Ok(Array::new(shape, data)) | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
@@ -2168,7 +2173,15 @@ impl Value { | |||||||||||||||||
| 0 => Err(env.error("Cannot pick random row of an empty array").fill()), | ||||||||||||||||||
| 1 => Ok(self.row(0)), | ||||||||||||||||||
| len => { | ||||||||||||||||||
| let i = RNG.with_borrow_mut(|rng| rng.gen_range(0..len)); | ||||||||||||||||||
| let i = RNG.with_borrow_mut(|rng| { | ||||||||||||||||||
| let upper = len.next_power_of_two(); | ||||||||||||||||||
| loop { | ||||||||||||||||||
| let r = rng.next_u64() as usize % upper; | ||||||||||||||||||
| if r < len { | ||||||||||||||||||
| break r; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+2177
to
+2183
|
||||||||||||||||||
| let upper = len.next_power_of_two(); | |
| loop { | |
| let r = rng.next_u64() as usize % upper; | |
| if r < len { | |
| break r; | |
| } | |
| } | |
| ((rng.next_u64() as u128 * len as u128) >> 64) as usize |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||||||||||||||||
| use std::{cmp::Ordering, ptr}; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| use ecow::EcoVec; | ||||||||||||||||||||
| use rand::Rng; | ||||||||||||||||||||
| use rand_xoshiro::rand_core::RngCore; | ||||||||||||||||||||
| use rayon::prelude::*; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| use crate::{algorithm::ArrayCmpSlice, random_with, val_as_arr, Array, ArrayValue, Value}; | ||||||||||||||||||||
|
|
@@ -232,7 +232,16 @@ impl<T: ArrayValue> Array<T> { | |||||||||||||||||||
| let row_len = self.row_len(); | ||||||||||||||||||||
| let slice = self.data.as_mut_slice(); | ||||||||||||||||||||
| for i in (1..row_count).rev() { | ||||||||||||||||||||
| let j = rng.gen_range(0..=i); | ||||||||||||||||||||
| let j = { | ||||||||||||||||||||
| let upper = i.next_power_of_two(); | ||||||||||||||||||||
| loop { | ||||||||||||||||||||
| let r = rng.next_u64() as usize % upper; | ||||||||||||||||||||
| if r <= i { | ||||||||||||||||||||
| break r; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+236
to
+242
|
||||||||||||||||||||
| let upper = i.next_power_of_two(); | |
| loop { | |
| let r = rng.next_u64() as usize % upper; | |
| if r <= i { | |
| break r; | |
| } | |
| } | |
| // Use constant-time unbiased sampling | |
| ((rng.next_u64() as u128 * (i as u128 + 1)) >> 64) as usize |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,10 @@ use std::{ | |||||
| }; | ||||||
|
|
||||||
| use ecow::EcoVec; | ||||||
| use rand::prelude::*; | ||||||
| use rand_xoshiro::{ | ||||||
| rand_core::{RngCore, SeedableRng}, | ||||||
| Xoshiro256Plus, | ||||||
| }; | ||||||
|
|
||||||
| use crate::{ | ||||||
| parse_doc_line_fragments, Array, Boxed, PrimDocFragment, SysBackend, Value, WILDCARD_NAN, | ||||||
|
|
@@ -485,7 +488,9 @@ fn music_constant(backend: &dyn SysBackend) -> Value { | |||||
| hat_mask.push((hat_bits & 1) as f64); | ||||||
| hat_bits >>= 1; | ||||||
| } | ||||||
| let mut rng = SmallRng::seed_from_u64(0); | ||||||
| let mut rng = Xoshiro256Plus::seed_from_u64(0); | ||||||
| let mut rand_wrench = | ||||||
| || 2.0 * f64::from_bits(rng.next_u64() >> 12 | 0x3FF0_0000_0000_0000) - 3.0; // gen_range(-1..=1) | ||||||
|
||||||
| || 2.0 * f64::from_bits(rng.next_u64() >> 12 | 0x3FF0_0000_0000_0000) - 3.0; // gen_range(-1..=1) | |
| || 2.0 * f64::from_bits(rng.next_u64() >> 12 | 0x3FF0_0000_0000_0000) - 2.0; // gen_range(-1..=1) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,7 +20,10 @@ use std::{ | |||||
| }, | ||||||
| }; | ||||||
|
|
||||||
| use rand::prelude::*; | ||||||
| use rand_xoshiro::{ | ||||||
| rand_core::{RngCore, SeedableRng}, | ||||||
| Xoshiro256Plus, | ||||||
| }; | ||||||
|
|
||||||
| use crate::{ | ||||||
| algorithm::{self, ga::GaOp, loops, reduce, table, zip, *}, | ||||||
|
|
@@ -1783,22 +1786,22 @@ fn undo_regex(env: &mut Uiua) -> UiuaResult { | |||||
| } | ||||||
|
|
||||||
| thread_local! { | ||||||
| pub(crate) static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy()); | ||||||
| pub(crate) static RNG: RefCell<Xoshiro256Plus> = RefCell::new(Xoshiro256Plus::seed_from_u64(f64::to_bits(crate::now()))) | ||||||
|
||||||
| pub(crate) static RNG: RefCell<Xoshiro256Plus> = RefCell::new(Xoshiro256Plus::seed_from_u64(f64::to_bits(crate::now()))) | |
| pub(crate) static RNG: RefCell<Xoshiro256Plus> = RefCell::new(Xoshiro256Plus::seed_from_u64(crate::now() as u64)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Fisher-Yates shuffle implementation is incorrect. The random index should be selected from the range
i..l(remaining elements), not0..l. The current implementation can swap with already-shuffled elements, breaking uniformity. Change line 275-280 to generate a random number in the range[i, l)instead.