Skip to content

Commit

Permalink
Update "rand" to 0.8 and "rand_distr" to 0.4. (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghuls committed Nov 11, 2021
1 parent dab34d3 commit ad111d3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
4 changes: 2 additions & 2 deletions polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ num = "^0.4"
num_cpus = "1.1"
polars-arrow = { version = "0.17.0", path = "../polars-arrow" }
prettytable-rs = { version = "0.8.0", optional = true }
rand = { version = "0.7", optional = true }
rand_distr = { version = "0.3", optional = true }
rand = { version = "0.8", optional = true }
rand_distr = { version = "0.4", optional = true }
rayon = "1.5"
regex = { version = "1.4", optional = true }
# activate if you want serde support for Series and DataFrames
Expand Down
25 changes: 11 additions & 14 deletions polars/polars-core/src/chunked_array/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ use rand::prelude::*;
use rand::seq::IteratorRandom;
use rand_distr::{Distribution, Normal, StandardNormal, Uniform};

fn create_rand_index_with_replacement(n: usize, len: usize) -> (ThreadRng, UInt32Chunked) {
fn create_rand_index_with_replacement(n: usize, len: usize) -> UInt32Chunked {
let mut rng = rand::thread_rng();
(
rng,
(0u32..n as u32)
.map(move |_| Uniform::new(0u32, len as u32).sample(&mut rng))
.collect_trusted::<NoNull<UInt32Chunked>>()
.into_inner(),
)
(0u32..n as u32)
.map(move |_| Uniform::new(0u32, len as u32).sample(&mut rng))
.collect_trusted::<NoNull<UInt32Chunked>>()
.into_inner()
}

fn create_rand_index_no_replacement(n: usize, len: usize) -> (ThreadRng, UInt32Chunked) {
fn create_rand_index_no_replacement(n: usize, len: usize) -> UInt32Chunked {
// TODO! prevent allocation.
let mut rng = rand::thread_rng();
let mut buf = AlignedVec::with_capacity(n);
// Safety: will be filled
unsafe { buf.set_len(n) };
(0u32..len as u32).choose_multiple_fill(&mut rng, buf.as_mut_slice());
(rng, UInt32Chunked::new_from_aligned_vec("", buf))
UInt32Chunked::new_from_aligned_vec("", buf)
}

impl<T> ChunkedArray<T>
Expand All @@ -42,13 +39,13 @@ where

match with_replacement {
true => {
let (_, idx) = create_rand_index_with_replacement(n, len);
let idx = create_rand_index_with_replacement(n, len);
// Safety we know that we never go out of bounds
debug_assert_eq!(len, self.len());
unsafe { Ok(self.take_unchecked((&idx).into())) }
}
false => {
let (_, idx) = create_rand_index_no_replacement(n, len);
let idx = create_rand_index_no_replacement(n, len);
// Safety we know that we never go out of bounds
debug_assert_eq!(len, self.len());
unsafe { Ok(self.take_unchecked((&idx).into())) }
Expand All @@ -73,8 +70,8 @@ impl DataFrame {
}
// all columns should used the same indices. So we first create the indices.
let idx: UInt32Chunked = match with_replacement {
true => create_rand_index_with_replacement(n, self.height()).1,
false => create_rand_index_no_replacement(n, self.height()).1,
true => create_rand_index_with_replacement(n, self.height()),
false => create_rand_index_no_replacement(n, self.height()),
};
// Safety:
// indices are within bounds
Expand Down
23 changes: 11 additions & 12 deletions py-polars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad111d3

Please sign in to comment.