Skip to content

Commit

Permalink
Add the thread_rng feature flag (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy authored Jan 9, 2025
1 parent afa24e4 commit 16eb7de
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).

You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.

## [0.9.0-beta.3] - 2025-01-03
- Add feature `thread_rng` (#1547)

## [0.9.0-beta.1] - 2024-11-30
- Bump `rand_core` version

Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand"
version = "0.9.0-beta.1"
version = "0.9.0-beta.3"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down Expand Up @@ -28,7 +28,7 @@ features = ["small_rng", "serde"]

[features]
# Meta-features:
default = ["std", "std_rng", "os_rng", "small_rng"]
default = ["std", "std_rng", "os_rng", "small_rng", "thread_rng"]
nightly = [] # some additions requiring nightly Rust
serde = ["dep:serde", "rand_core/serde"]

Expand All @@ -51,6 +51,9 @@ std_rng = ["dep:rand_chacha"]
# Option: enable SmallRng
small_rng = []

# Option: enable ThreadRng and rng()
thread_rng = ["std", "std_rng", "os_rng"]

# Option: use unbiased sampling for algorithms supporting this option: Uniform distribution.
# By default, bias affecting no more than one in 2^48 samples is accepted.
# Note: enabling this option is expected to affect reproducibility of results.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ rand = "0.8.5"
Or, to try the 0.9.0 beta release:
```toml
[dependencies]
rand = "=0.9.0-beta.1"
rand = "=0.9.0-beta.3"
```

To get started using Rand, see [The Book](https://rust-random.github.io/book).
Expand Down
4 changes: 2 additions & 2 deletions distr_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"
publish = false

[dev-dependencies]
rand_distr = { path = "../rand_distr", version = "=0.5.0-beta.2", default-features = false, features = ["alloc"] }
rand = { path = "..", version = "=0.9.0-beta.1", features = ["small_rng"] }
rand_distr = { path = "../rand_distr", version = "=0.5.0-beta.3", default-features = false, features = ["alloc"] }
rand = { path = "..", version = "=0.9.0-beta.3", features = ["small_rng"] }
num-traits = "0.2.19"
# Special functions for testing distributions
special = "0.11.0"
Expand Down
3 changes: 3 additions & 0 deletions rand_distr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0-beta.3] - 2025-01-03
- Bump `rand` version (#1547)

## [0.5.0-beta.2] - 2024-11-30
- Bump `rand` version

Expand Down
6 changes: 3 additions & 3 deletions rand_distr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rand_distr"
version = "0.5.0-beta.2"
version = "0.5.0-beta.3"
authors = ["The Rand Project Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down Expand Up @@ -33,15 +33,15 @@ std_math = ["num-traits/std"]
serde = ["dep:serde", "dep:serde_with", "rand/serde"]

[dependencies]
rand = { path = "..", version = "=0.9.0-beta.1", default-features = false }
rand = { path = "..", version = "=0.9.0-beta.3", default-features = false }
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
serde = { version = "1.0.103", features = ["derive"], optional = true }
serde_with = { version = ">= 3.0, <= 3.11", optional = true }

[dev-dependencies]
rand_pcg = { version = "=0.9.0-beta.1", path = "../rand_pcg" }
# For inline examples
rand = { path = "..", version = "=0.9.0-beta.1", features = ["small_rng"] }
rand = { path = "..", version = "=0.9.0-beta.3", features = ["small_rng"] }
# Histogram implementation for testing uniformity
average = { version = "0.15", features = [ "std" ] }
# Special functions for testing distributions
Expand Down
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ pub mod rngs;
pub mod seq;

// Public exports
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
pub use crate::rngs::thread::rng;

/// Access the thread-local generator
///
/// Use [`rand::rng()`](rng()) instead.
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
#[deprecated(since = "0.9.0", note = "renamed to `rng`")]
#[inline]
pub fn thread_rng() -> crate::rngs::ThreadRng {
Expand All @@ -118,7 +118,7 @@ pub fn thread_rng() -> crate::rngs::ThreadRng {

pub use rng::{Fill, Rng};

#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
use crate::distr::{Distribution, StandardUniform};

/// Generate a random value using the thread-local random number generator.
Expand Down Expand Up @@ -159,7 +159,7 @@ use crate::distr::{Distribution, StandardUniform};
///
/// [`StandardUniform`]: distr::StandardUniform
/// [`ThreadRng`]: rngs::ThreadRng
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
#[inline]
pub fn random<T>() -> T
where
Expand All @@ -179,7 +179,7 @@ where
/// let v: Vec<i32> = rand::random_iter().take(5).collect();
/// println!("{v:?}");
/// ```
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
#[inline]
pub fn random_iter<T>() -> distr::DistIter<StandardUniform, rngs::ThreadRng, T>
where
Expand All @@ -204,7 +204,7 @@ where
/// ```
/// Note that the first example can also be achieved (without `collect`'ing
/// to a `Vec`) using [`seq::IteratorRandom::choose`].
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
#[inline]
pub fn random_range<T, R>(range: R) -> T
where
Expand All @@ -228,7 +228,7 @@ where
/// # Panics
///
/// If `p < 0` or `p > 1`.
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
#[inline]
#[track_caller]
pub fn random_bool(p: f64) -> bool {
Expand Down Expand Up @@ -260,7 +260,7 @@ pub fn random_bool(p: f64) -> bool {
/// ```
///
/// [`Bernoulli`]: distr::Bernoulli
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
#[inline]
#[track_caller]
pub fn random_ratio(numerator: u32, denominator: u32) -> bool {
Expand All @@ -282,7 +282,7 @@ pub fn random_ratio(numerator: u32, denominator: u32) -> bool {
/// Note that you can instead use [`random()`] to generate an array of random
/// data, though this is slower for small elements (smaller than the RNG word
/// size).
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
#[inline]
#[track_caller]
pub fn fill<T: Fill + ?Sized>(dest: &mut T) {
Expand All @@ -302,7 +302,7 @@ mod test {
}

#[test]
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
fn test_random() {
let _n: u64 = random();
let _f: f32 = random();
Expand All @@ -316,7 +316,7 @@ mod test {
}

#[test]
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
fn test_range() {
let _n: usize = random_range(42..=43);
let _f: f32 = random_range(42.0..43.0);
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use crate::rngs::SmallRng;
#[doc(no_inline)]
pub use crate::rngs::StdRng;
#[doc(no_inline)]
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
pub use crate::rngs::ThreadRng;
#[doc(no_inline)]
pub use crate::seq::{IndexedMutRandom, IndexedRandom, IteratorRandom, SliceRandom};
Expand Down
4 changes: 2 additions & 2 deletions src/rngs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ mod xoshiro256plusplus;

#[cfg(feature = "std_rng")]
mod std;
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
pub(crate) mod thread;

#[cfg(feature = "small_rng")]
pub use self::small::SmallRng;
#[cfg(feature = "std_rng")]
pub use self::std::StdRng;
#[cfg(all(feature = "std", feature = "std_rng", feature = "os_rng"))]
#[cfg(feature = "thread_rng")]
pub use self::thread::ThreadRng;

#[cfg(feature = "os_rng")]
Expand Down

0 comments on commit 16eb7de

Please sign in to comment.