Skip to content

Commit

Permalink
chore: Migrate to rand v0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Feb 20, 2025
1 parent fe94069 commit 820eebd
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 228 deletions.
177 changes: 129 additions & 48 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ parking_lot = "0.12"
percent-encoding = "2.3"
pin-project-lite = "0.2"
pyo3 = "0.23.4"
rand = "0.8"
rand_distr = "0.4"
rand = "0.9"
rand_distr = "0.5"
raw-cpuid = "11"
rayon = "1.9"
recursive = "0.1"
Expand Down
113 changes: 0 additions & 113 deletions crates/polars-arrow/src/bitmap/utils/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,117 +270,4 @@ mod tests {

assert_eq!(bitmap.set_bits(), 4);
}

#[test]
#[ignore = "Fuzz test. Too slow"]
fn test_fuzz_collect_into() {
for _ in 0..10_000 {
let mut set_bits = 0;
let mut unset_bits = 0;

let mut length = 0;
let mut pattern = Vec::new();
for _ in 0..rand::random::<usize>() % 1024 {
let bs = rand::random::<u8>() % 4;

let word = match bs {
0 => u64::MIN,
1 => u64::MAX,
2 | 3 => rand::random(),
_ => unreachable!(),
};

pattern.extend_from_slice(&word.to_le_bytes());
set_bits += word.count_ones();
unset_bits += word.count_zeros();
length += 64;
}

for _ in 0..rand::random::<usize>() % 7 {
let b = rand::random::<u8>();
pattern.push(b);
set_bits += b.count_ones();
unset_bits += b.count_zeros();
length += 8;
}

let last_length = rand::random::<usize>() % 8;
if last_length != 0 {
let b = rand::random::<u8>();
pattern.push(b);
let ones = (b & ((1 << last_length) - 1)).count_ones();
set_bits += ones;
unset_bits += last_length as u32 - ones;
length += last_length;
}

let mut iter = BitmapIter::new(&pattern, 0, length);
let mut bitmap = MutableBitmap::with_capacity(length);

while iter.num_remaining() > 0 {
let len_before = bitmap.len();
let n = rand::random::<usize>() % iter.num_remaining();
iter.collect_n_into(&mut bitmap, n);

// Ensure we are booking the progress we expect
assert_eq!(bitmap.len(), len_before + n);
}

let bitmap = bitmap.freeze();

assert_eq!(bitmap.set_bits(), set_bits as usize);
assert_eq!(bitmap.unset_bits(), unset_bits as usize);
}
}

#[test]
#[ignore = "Fuzz test. Too slow"]
fn test_fuzz_leading_ops() {
for _ in 0..10_000 {
let mut length = 0;
let mut pattern = Vec::new();
for _ in 0..rand::random::<usize>() % 1024 {
let bs = rand::random::<u8>() % 4;

let word = match bs {
0 => u64::MIN,
1 => u64::MAX,
2 | 3 => rand::random(),
_ => unreachable!(),
};

pattern.extend_from_slice(&word.to_le_bytes());
length += 64;
}

for _ in 0..rand::random::<usize>() % 7 {
pattern.push(rand::random::<u8>());
length += 8;
}

let last_length = rand::random::<usize>() % 8;
if last_length != 0 {
pattern.push(rand::random::<u8>());
length += last_length;
}

let mut iter = BitmapIter::new(&pattern, 0, length);

let mut prev_remaining = iter.num_remaining();
while iter.num_remaining() != 0 {
let num_ones = iter.clone().take_leading_ones();
assert_eq!(num_ones, (&mut iter).take_while(|&b| b).count());

let num_zeros = iter.clone().take_leading_zeros();
assert_eq!(num_zeros, (&mut iter).take_while(|&b| !b).count());

// Ensure that we are making progress
assert!(iter.num_remaining() < prev_remaining);
prev_remaining = iter.num_remaining();
}

assert_eq!(iter.take_leading_zeros(), 0);
assert_eq!(iter.take_leading_ones(), 0);
}
}
}
12 changes: 6 additions & 6 deletions crates/polars-arrow/src/bitmap/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ mod tests {
#[ignore = "Fuzz test. Too slow"]
#[test]
fn leading_trailing_fuzz() {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();

const SIZE: usize = 1000;
const REPEATS: usize = 10_000;
Expand All @@ -264,16 +264,16 @@ mod tests {

for _ in 0..REPEATS {
v.clear();
let offset = rng.gen_range(0..SIZE);
let length = rng.gen_range(0..SIZE - offset);
let extra_padding = rng.gen_range(0..64);
let offset = rng.random_range(0..SIZE);
let length = rng.random_range(0..SIZE - offset);
let extra_padding = rng.random_range(0..64);

let mut num_remaining = usize::min(SIZE, offset + length + extra_padding);
while num_remaining > 0 {
let chunk_size = rng.gen_range(1..=num_remaining);
let chunk_size = rng.random_range(1..=num_remaining);
v.extend(
rng.clone()
.sample_iter(rand::distributions::Slice::new(&[false, true]).unwrap())
.sample_iter(rand::distr::slice::Choose::new(&[false, true]).unwrap())
.take(chunk_size),
);
num_remaining -= chunk_size;
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-compute/src/filter/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ mod test {
// Verify polyfill against naive implementation.
let mut rng = StdRng::seed_from_u64(0xdeadbeef);
for _ in 0..100 {
let x = rng.gen();
let y = rng.gen();
let x = rng.random();
let y = rng.random();
assert_eq!(naive_pext64(x, y), pext64_polyfill(x, y, y.count_ones()));

// Test all-zeros and all-ones.
Expand All @@ -282,9 +282,9 @@ mod test {
assert_eq!(naive_pext64(x, u64::MAX), pext64_polyfill(x, u64::MAX, 64));

// Test low popcount mask.
let popcnt = rng.gen_range(0..=8);
let popcnt = rng.random_range(0..=8);
// Not perfect (can generate same bit twice) but it'll do.
let mask = (0..popcnt).map(|_| 1 << rng.gen_range(0..64)).sum();
let mask = (0..popcnt).map(|_| 1 << rng.random_range(0..64)).sum();
assert_eq!(
naive_pext64(x, mask),
pext64_polyfill(x, mask, mask.count_ones())
Expand Down
24 changes: 12 additions & 12 deletions crates/polars-core/src/chunked_array/random.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use num_traits::{Float, NumCast};
use polars_error::to_compute_err;
use rand::distributions::Bernoulli;
use rand::distr::Bernoulli;
use rand::prelude::*;
use rand::seq::index::IndexVec;
use rand_distr::{Normal, Standard, StandardNormal, Uniform};
use rand_distr::{Distribution, Normal, StandardNormal, StandardUniform, Uniform};

use crate::prelude::DataType::Float64;
use crate::prelude::*;
Expand All @@ -15,7 +15,7 @@ fn create_rand_index_with_replacement(n: usize, len: usize, seed: Option<u64>) -
return IdxCa::new_vec(PlSmallStr::EMPTY, vec![]);
}
let mut rng = SmallRng::seed_from_u64(seed.unwrap_or_else(get_global_random_u64));
let dist = Uniform::new(0, len as IdxSize);
let dist = Uniform::new(0, len as IdxSize).unwrap();
(0..n as IdxSize)
.map(move |_| dist.sample(&mut rng))
.collect_trusted::<NoNull<IdxCa>>()
Expand All @@ -42,7 +42,7 @@ fn create_rand_index_no_replacement(
// size returned.
buf = match rand::seq::index::sample(&mut rng, len, n) {
IndexVec::U32(v) => v.into_iter().map(|x| x as IdxSize).collect(),
IndexVec::USize(v) => v.into_iter().map(|x| x as IdxSize).collect(),
IndexVec::U64(v) => v.into_iter().map(|x| x as IdxSize).collect(),
};
}
IdxCa::new_vec(PlSmallStr::EMPTY, buf)
Expand All @@ -51,16 +51,16 @@ fn create_rand_index_no_replacement(
impl<T> ChunkedArray<T>
where
T: PolarsNumericType,
Standard: Distribution<T::Native>,
StandardUniform: Distribution<T::Native>,
{
pub fn init_rand(size: usize, null_density: f32, seed: Option<u64>) -> Self {
let mut rng = SmallRng::seed_from_u64(seed.unwrap_or_else(get_global_random_u64));
(0..size)
.map(|_| {
if rng.gen::<f32>() < null_density {
if rng.random::<f32>() < null_density {
None
} else {
Some(rng.gen())
Some(rng.random())
}
})
.collect()
Expand Down Expand Up @@ -253,7 +253,7 @@ where
) -> PolarsResult<Self> {
let normal = Normal::new(mean, std_dev).map_err(to_compute_err)?;
let mut builder = PrimitiveChunkedBuilder::<T>::new(name, length);
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
for _ in 0..length {
let smpl = normal.sample(&mut rng);
let smpl = NumCast::from(smpl).unwrap();
Expand All @@ -265,7 +265,7 @@ where
/// Create [`ChunkedArray`] with samples from a Standard Normal distribution.
pub fn rand_standard_normal(name: PlSmallStr, length: usize) -> Self {
let mut builder = PrimitiveChunkedBuilder::<T>::new(name, length);
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
for _ in 0..length {
let smpl: f64 = rng.sample(StandardNormal);
let smpl = NumCast::from(smpl).unwrap();
Expand All @@ -276,9 +276,9 @@ where

/// Create [`ChunkedArray`] with samples from a Uniform distribution.
pub fn rand_uniform(name: PlSmallStr, length: usize, low: f64, high: f64) -> Self {
let uniform = Uniform::new(low, high);
let uniform = Uniform::new(low, high).unwrap();
let mut builder = PrimitiveChunkedBuilder::<T>::new(name, length);
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
for _ in 0..length {
let smpl = uniform.sample(&mut rng);
let smpl = NumCast::from(smpl).unwrap();
Expand All @@ -292,7 +292,7 @@ impl BooleanChunked {
/// Create [`ChunkedArray`] with samples from a Bernoulli distribution.
pub fn rand_bernoulli(name: PlSmallStr, length: usize, p: f64) -> PolarsResult<Self> {
let dist = Bernoulli::new(p).map_err(to_compute_err)?;
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let mut builder = BooleanChunkedBuilder::new(name, length);
for _ in 0..length {
let smpl = dist.sample(&mut rng);
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
use rand::prelude::*;

static POLARS_GLOBAL_RNG_STATE: Lazy<Mutex<SmallRng>> =
Lazy::new(|| Mutex::new(SmallRng::from_entropy()));
Lazy::new(|| Mutex::new(SmallRng::from_os_rng()));

pub(crate) fn get_global_random_u64() -> u64 {
POLARS_GLOBAL_RNG_STATE.lock().unwrap().next_u64()
Expand Down
14 changes: 7 additions & 7 deletions crates/polars-ops/src/chunked_array/gather_skip_nulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where
mod test {
use std::ops::Range;

use rand::distributions::uniform::SampleUniform;
use rand::distr::uniform::SampleUniform;
use rand::prelude::*;

use super::*;
Expand All @@ -164,13 +164,13 @@ mod test {
val: Range<T>,
len_range: Range<usize>,
) -> Vec<T> {
let n = rng.gen_range(len_range);
(0..n).map(|_| rng.gen_range(val.clone())).collect()
let n = rng.random_range(len_range);
(0..n).map(|_| rng.random_range(val.clone())).collect()
}

fn random_filter<T: Clone, R: Rng>(rng: &mut R, v: &[T], pr: Range<f64>) -> Vec<Option<T>> {
let p = rng.gen_range(pr);
let rand_filter = |x| Some(x).filter(|_| rng.gen::<f64>() < p);
let p = rng.random_range(pr);
let rand_filter = |x| Some(x).filter(|_| rng.random::<f64>() < p);
v.iter().cloned().map(rand_filter).collect()
}

Expand Down Expand Up @@ -203,12 +203,12 @@ mod test {
let mut rng = SmallRng::seed_from_u64(0xdeadbeef);

for _test in 0..20 {
let num_elem_chunks = rng.gen_range(1..10);
let num_elem_chunks = rng.random_range(1..10);
let elem_chunks: Vec<_> = (0..num_elem_chunks).map(|_| random_vec(&mut rng, 0..u32::MAX, 0..100)).collect();
let null_elem_chunks: Vec<_> = elem_chunks.iter().map(|c| random_filter(&mut rng, c, 0.7..1.0)).collect();
let num_nonnull_elems: usize = null_elem_chunks.iter().map(|c| c.iter().filter(|x| x.is_some()).count()).sum();

let num_idx_chunks = rng.gen_range(1..10);
let num_idx_chunks = rng.random_range(1..10);
let idx_chunks: Vec<_> = (0..num_idx_chunks).map(|_| random_vec(&mut rng, 0..num_nonnull_elems as IdxSize, 0..200)).collect();
let null_idx_chunks: Vec<_> = idx_chunks.iter().map(|c| random_filter(&mut rng, c, 0.7..1.0)).collect();

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-ops/src/series/ops/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Default for RankOptions {

#[cfg(feature = "random")]
fn get_random_seed() -> u64 {
let mut rng = SmallRng::from_entropy();
let mut rng = SmallRng::from_os_rng();

rng.next_u64()
}
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-sql/src/sql_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use polars_lazy::prelude::*;
use polars_plan::prelude::typed_lit;
use polars_plan::prelude::LiteralValue::Null;
use polars_time::Duration;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use rand::distr::Alphanumeric;
use rand::{rng, Rng};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use sqlparser::ast::{
Expand Down Expand Up @@ -267,7 +267,7 @@ impl SQLExprVisitor<'_> {
if schema.len() != 1 {
polars_bail!(SQLSyntax: "SQL subquery returns more than one column");
}
let rand_string: String = thread_rng()
let rand_string: String = rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
Expand Down
10 changes: 5 additions & 5 deletions crates/polars-stream/src/async_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl Executor {
fn runner(&self, thread: usize) {
TLS_THREAD_ID.set(thread);

let mut rng = SmallRng::from_rng(&mut rand::thread_rng()).unwrap();
let mut rng = SmallRng::from_rng(&mut rand::rng());
let mut worker = self.park_group.new_worker();
let mut last_block_start = None;

Expand Down Expand Up @@ -420,10 +420,10 @@ fn random_permutation<R: Rng>(len: u32, rng: &mut R) -> impl Iterator<Item = u32
let modulus = len.next_power_of_two();
let halfwidth = modulus.trailing_zeros() / 2;
let mask = modulus - 1;
let displace_zero = rng.gen::<u32>();
let odd1 = rng.gen::<u32>() | 1;
let odd2 = rng.gen::<u32>() | 1;
let uniform_first = ((rng.gen::<u32>() as u64 * len as u64) >> 32) as u32;
let displace_zero = rng.random::<u32>();
let odd1 = rng.random::<u32>() | 1;
let odd2 = rng.random::<u32>() | 1;
let uniform_first = ((rng.random::<u32>() as u64 * len as u64) >> 32) as u32;

(0..modulus)
.map(move |mut i| {
Expand Down
Loading

0 comments on commit 820eebd

Please sign in to comment.