Skip to content

Commit 20afad9

Browse files
committed
merge master
1 parent 07b3ee7 commit 20afad9

32 files changed

+990
-218
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,28 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1414
- Added a `serde1` feature and added Serialize/Deserialize to `UniformInt` and `WeightedIndex` (#974)
1515
- Document types supported by `random` (#994)
1616
- Implement weighted sampling without replacement (#976, #1013)
17+
- Add `IteratorRandom::choose_stable` as an alternative to `choose` which does not depend on size hints (#1057)
1718

1819
### Changes
20+
- `getrandom` updated to v0.2 (#1041)
21+
- `ThreadRng` is no longer `Copy` to enable safe usage within thread-local destructors (see #968)
1922
- `gen_range(a, b)` was replaced with `gen_range(a..b)`, and `gen_range(a..=b)`
2023
is supported (#744, #1003). Note that `a` and `b` can no longer be references or SIMD types.
2124
- Replace `AsByteSliceMut` with `Fill` (#940)
2225
- Move alias method for `WeightedIndex` to `rand_distr` (#945)
2326
- `Alphanumeric` samples bytes instead of chars (#935)
2427
- The minimum supported Rust version is now 1.36 (#1011)
28+
- Restrict `rand::rngs::adapter` to `std` (#1027)
2529
- Better NaN handling for `WeightedIndex` (#1005)
2630
- Implement `IntoIterator` for `IndexVec`, replacing the `into_iter` method (#1007)
2731
- Reduce packaged crate size (#983)
28-
- Drop some unsafe code (#962, #963)
32+
- Drop some unsafe code (#962, #963, #1011)
2933
- Improve treatment of rounding errors in `WeightedIndex::update_weights` (#956)
34+
- `StdRng`: Switch from ChaCha20 to ChaCha12 for better performance (#1028)
35+
- `SmallRng`: Replace PCG algorithm with xoshiro{128,256}++ (#1038)
36+
- The `nightly` feature no longer implies the `simd_support` feature (#1048)
37+
- Fix `simd_support` feature to work on current nightlies (#1056)
38+
- Improve accuracy and performance of `IteratorRandom::choose` (#1059)
3039

3140
## [0.7.3] - 2020-01-10
3241
### Fixes

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ appveyor = { repository = "rust-random/rand" }
2323
[features]
2424
# Meta-features:
2525
default = ["std", "std_rng"]
26-
nightly = ["simd_support"] # enables all features requiring nightly rust
26+
nightly = [] # enables performance optimizations requiring nightly rust
2727
serde1 = ["serde"]
2828

2929
# Option (enabled by default): without "std" rand uses libcore; this option
@@ -43,7 +43,7 @@ simd_support = ["packed_simd"]
4343
std_rng = ["rand_chacha", "rand_hc"]
4444

4545
# Option: enable SmallRng
46-
small_rng = ["rand_pcg"]
46+
small_rng = []
4747

4848
[workspace]
4949
members = [
@@ -56,14 +56,13 @@ members = [
5656

5757
[dependencies]
5858
rand_core = { path = "rand_core", version = "0.5.1" }
59-
rand_pcg = { path = "rand_pcg", version = "0.2.1", optional = true }
6059
log = { version = "0.4.4", optional = true }
6160
serde = { version = "1.0.103", features = ["derive"], optional = true }
6261

6362
[dependencies.packed_simd]
6463
# NOTE: so far no version works reliably due to dependence on unstable features
65-
version = "0.3"
66-
# git = "https://github.com/rust-lang-nursery/packed_simd"
64+
package = "packed_simd_2"
65+
version = "0.3.4"
6766
optional = true
6867
features = ["into_bits"]
6968

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,13 @@ Optionally, the following dependencies can be enabled:
103103
Additionally, these features configure Rand:
104104

105105
- `small_rng` enables inclusion of the `SmallRng` PRNG
106-
- `nightly` enables all experimental features
106+
- `nightly` enables some optimizations requiring nightly Rust
107107
- `simd_support` (experimental) enables sampling of SIMD values
108-
(uniformly random SIMD integers and floats)
108+
(uniformly random SIMD integers and floats), requiring nightly Rust
109+
110+
Note that nightly features are not stable and therefore not all library and
111+
compiler versions will be compatible. This is especially true of Rand's
112+
experimental `simd_support` feature.
109113

110114
Rand supports limited functionality in `no_std` mode (enabled via
111115
`default-features = false`). In this case, `OsRng` and `from_entropy` are

rand_chacha/src/guts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ impl ChaCha {
101101
}
102102
}
103103

104+
#[allow(clippy::many_single_char_names)]
104105
#[inline(always)]
105106
fn refill_wide_impl<Mach: Machine>(
106107
m: Mach, state: &mut ChaCha, drounds: u32, out: &mut [u8; BUFSZ],

rand_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ serde1 = ["serde"] # enables serde for BlockRng wrapper
2525

2626
[dependencies]
2727
serde = { version = "1", features = ["derive"], optional = true }
28-
getrandom = { version = "0.1", optional = true }
28+
getrandom = { version = "0.2", optional = true }
2929

3030
[package.metadata.docs.rs]
3131
# To build locally:

rand_core/src/error.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ pub struct Error {
2828
impl Error {
2929
/// Codes at or above this point can be used by users to define their own
3030
/// custom errors.
31+
///
32+
/// This is identical to [`getrandom::Error::CUSTOM_START`](https://docs.rs/getrandom/latest/getrandom/struct.Error.html#associatedconstant.CUSTOM_START).
3133
pub const CUSTOM_START: u32 = (1 << 31) + (1 << 30);
3234
/// Codes below this point represent OS Errors (i.e. positive i32 values).
3335
/// Codes at or above this point, but below [`Error::CUSTOM_START`] are
3436
/// reserved for use by the `rand` and `getrandom` crates.
37+
///
38+
/// This is identical to [`getrandom::Error::INTERNAL_START`](https://docs.rs/getrandom/latest/getrandom/struct.Error.html#associatedconstant.INTERNAL_START).
3539
pub const INTERNAL_START: u32 = 1 << 31;
3640

3741
/// Construct from any type supporting `std::error::Error`
@@ -208,3 +212,14 @@ impl fmt::Display for ErrorCode {
208212

209213
#[cfg(feature = "std")]
210214
impl std::error::Error for ErrorCode {}
215+
216+
#[cfg(test)]
217+
mod test {
218+
#[cfg(feature = "getrandom")]
219+
#[test]
220+
fn test_error_codes() {
221+
// Make sure the values are the same as in `getrandom`.
222+
assert_eq!(super::Error::CUSTOM_START, getrandom::Error::CUSTOM_START);
223+
assert_eq!(super::Error::INTERNAL_START, getrandom::Error::INTERNAL_START);
224+
}
225+
}

rand_core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#![deny(missing_docs)]
3636
#![deny(missing_debug_implementations)]
3737
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
38-
#![allow(clippy::unreadable_literal)]
3938
#![cfg_attr(doc_cfg, feature(doc_cfg))]
4039
#![no_std]
4140

rand_distr/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
- New `Beta` sampling algorithm for improved performance and accuracy (#1000)
9+
- `Normal` and `LogNormal` now support `from_mean_cv` and `from_zscore` (#1044)
10+
- Variants of `NormalError` changed (#1044)
11+
712
## [0.3.0] - 2020-08-25
813
- Move alias method for `WeightedIndex` from `rand` (#945)
914
- Rename `WeightedIndex` to `WeightedAliasIndex` (#1008)
@@ -12,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1217
- Remove `Distribution<u64>` impl for `Poisson` (#987)
1318
- Tweak `Dirichlet` and `alias_method` to use boxed slice instead of `Vec` (#987)
1419
- Use whitelist for package contents, reducing size by 5kb (#983)
15-
- Add case `lambda = 0` in the parametrixation of `Exp` (#972)
20+
- Add case `lambda = 0` in the parametrization of `Exp` (#972)
1621
- Implement inverse Gaussian distribution (#954)
1722
- Reformatting and use of `rustfmt::skip` (#926)
1823
- All error types now implement `std::error::Error` (#919)

rand_distr/benches/distributions.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::mem::size_of;
2020
use test::Bencher;
2121

2222
use rand::prelude::*;
23-
use rand_distr::{weighted::WeightedIndex, *};
23+
use rand_distr::*;
2424

2525
// At this time, distributions are optimised for 64-bit platforms.
2626
use rand_pcg::Pcg64Mcg;
@@ -112,11 +112,15 @@ distr_float!(distr_normal, f64, Normal::new(-1.23, 4.56).unwrap());
112112
distr_float!(distr_log_normal, f64, LogNormal::new(-1.23, 4.56).unwrap());
113113
distr_float!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0).unwrap());
114114
distr_float!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0).unwrap());
115+
distr_float!(distr_beta_small_param, f64, Beta::new(0.1, 0.1).unwrap());
116+
distr_float!(distr_beta_large_param_similar, f64, Beta::new(101., 95.).unwrap());
117+
distr_float!(distr_beta_large_param_different, f64, Beta::new(10., 1000.).unwrap());
118+
distr_float!(distr_beta_mixed_param, f64, Beta::new(0.5, 100.).unwrap());
115119
distr_float!(distr_cauchy, f64, Cauchy::new(4.2, 6.9).unwrap());
116120
distr_float!(distr_triangular, f64, Triangular::new(0., 1., 0.9).unwrap());
117121
distr_int!(distr_binomial, u64, Binomial::new(20, 0.7).unwrap());
118122
distr_int!(distr_binomial_small, u64, Binomial::new(1000000, 1e-30).unwrap());
119-
distr_int!(distr_poisson, u64, Poisson::new(4.0).unwrap());
123+
distr_float!(distr_poisson, f64, Poisson::new(4.0).unwrap());
120124
distr!(distr_bernoulli, bool, Bernoulli::new(0.18).unwrap());
121125
distr_arr!(distr_circle, [f64; 2], UnitCircle);
122126
distr_arr!(distr_sphere, [f64; 3], UnitSphere);
@@ -127,10 +131,10 @@ distr_int!(distr_weighted_u32, usize, WeightedIndex::new(&[1u32, 2, 3, 4, 12, 0,
127131
distr_int!(distr_weighted_f64, usize, WeightedIndex::new(&[1.0f64, 0.001, 1.0/3.0, 4.01, 0.0, 3.3, 22.0, 0.001]).unwrap());
128132
distr_int!(distr_weighted_large_set, usize, WeightedIndex::new((0..10000).rev().chain(1..10001)).unwrap());
129133

130-
distr_int!(distr_weighted_alias_method_i8, usize, weighted::alias_method::WeightedIndex::new(vec![1i8, 2, 3, 4, 12, 0, 2, 1]).unwrap());
131-
distr_int!(distr_weighted_alias_method_u32, usize, weighted::alias_method::WeightedIndex::new(vec![1u32, 2, 3, 4, 12, 0, 2, 1]).unwrap());
132-
distr_int!(distr_weighted_alias_method_f64, usize, weighted::alias_method::WeightedIndex::new(vec![1.0f64, 0.001, 1.0/3.0, 4.01, 0.0, 3.3, 22.0, 0.001]).unwrap());
133-
distr_int!(distr_weighted_alias_method_large_set, usize, weighted::alias_method::WeightedIndex::new((0..10000).rev().chain(1..10001).collect()).unwrap());
134+
distr_int!(distr_weighted_alias_method_i8, usize, WeightedAliasIndex::new(vec![1i8, 2, 3, 4, 12, 0, 2, 1]).unwrap());
135+
distr_int!(distr_weighted_alias_method_u32, usize, WeightedAliasIndex::new(vec![1u32, 2, 3, 4, 12, 0, 2, 1]).unwrap());
136+
distr_int!(distr_weighted_alias_method_f64, usize, WeightedAliasIndex::new(vec![1.0f64, 0.001, 1.0/3.0, 4.01, 0.0, 3.3, 22.0, 0.001]).unwrap());
137+
distr_int!(distr_weighted_alias_method_large_set, usize, WeightedAliasIndex::new((0..10000).rev().chain(1..10001).collect()).unwrap());
134138

135139

136140
#[bench]

rand_distr/src/binomial.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use crate::{Distribution, Uniform};
1313
use rand::Rng;
1414
use core::fmt;
15+
use core::cmp::Ordering;
1516

1617
/// The binomial distribution `Binomial(n, p)`.
1718
///
@@ -210,24 +211,28 @@ impl Distribution<u64> for Binomial {
210211
let s = p / q;
211212
let a = s * (n + 1.);
212213
let mut f = 1.0;
213-
if m < y {
214-
let mut i = m;
215-
loop {
216-
i += 1;
217-
f *= a / (i as f64) - s;
218-
if i == y {
219-
break;
214+
match m.cmp(&y) {
215+
Ordering::Less => {
216+
let mut i = m;
217+
loop {
218+
i += 1;
219+
f *= a / (i as f64) - s;
220+
if i == y {
221+
break;
222+
}
220223
}
221-
}
222-
} else if m > y {
223-
let mut i = y;
224-
loop {
225-
i += 1;
226-
f /= a / (i as f64) - s;
227-
if i == m {
228-
break;
224+
},
225+
Ordering::Greater => {
226+
let mut i = y;
227+
loop {
228+
i += 1;
229+
f /= a / (i as f64) - s;
230+
if i == m {
231+
break;
232+
}
229233
}
230-
}
234+
},
235+
Ordering::Equal => {},
231236
}
232237
if v > f {
233238
continue;

0 commit comments

Comments
 (0)