-
-
Notifications
You must be signed in to change notification settings - Fork 434
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
Remove SmallRng::from_thread_rng #1532
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b8de33
Revise SmallRng docs
dhardy 70d8a53
Remove SmallRng::from_thread_rng
dhardy 36e60c6
rand_pcg: update docs on seeding
dhardy 7aa9ae0
rand_chacha: add example usage of from_rng
dhardy 27ee112
benches: prefer usage of from_rng(&mut rand::rng()) over from_os_rng()
dhardy f3c50db
More doc tweaks
dhardy d530ee8
Tweak docs
dhardy 7e0b3fd
Merge branch 'master' into rm_from_thread_rng
dhardy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,24 +40,22 @@ type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus; | |
/// suitable for seeding, but note that, even with a fixed seed, output is not | ||
/// [portable]. Some suggestions: | ||
/// | ||
/// 1. Seed **from an integer** via `seed_from_u64`. This uses a hash function | ||
/// internally to yield a (typically) good seed from any input. | ||
/// 1. To automatically seed with a unique seed, use [`SeedableRng::from_rng`]: | ||
/// ``` | ||
/// # use rand::{SeedableRng, rngs::SmallRng}; | ||
/// let rng = SmallRng::seed_from_u64(1); | ||
/// use rand::SeedableRng; | ||
/// use rand::rngs::SmallRng; | ||
/// let rng = SmallRng::from_rng(&mut rand::rng()); | ||
/// # let _: SmallRng = rng; | ||
/// ``` | ||
/// 2. With a fresh seed, **direct from the OS** (implies a syscall): | ||
/// 2. To use a deterministic integral seed, use `seed_from_u64`. This uses a | ||
/// hash function internally to yield a (typically) good seed from any | ||
/// input. | ||
/// ``` | ||
/// # use rand::{SeedableRng, rngs::SmallRng}; | ||
/// let rng = SmallRng::from_os_rng(); | ||
/// let rng = SmallRng::seed_from_u64(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can keep the mention of |
||
/// # let _: SmallRng = rng; | ||
/// ``` | ||
/// 3. Via [`SmallRng::from_thread_rng`]: | ||
/// ``` | ||
/// # use rand::rngs::SmallRng; | ||
/// let rng = SmallRng::from_thread_rng(); | ||
/// ``` | ||
/// 3. To seed deterministically from text or other input, use [`rand_seeder`]. | ||
/// | ||
/// See also [Seeding RNGs] in the book. | ||
/// | ||
|
@@ -74,6 +72,7 @@ type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus; | |
/// [rand_pcg]: https://crates.io/crates/rand_pcg | ||
/// [rand_xoshiro]: https://crates.io/crates/rand_xoshiro | ||
/// [`rand_chacha::ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html | ||
/// [`rand_seeder`]: https://docs.rs/rand_seeder/latest/rand_seeder/ | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct SmallRng(Rng); | ||
|
||
|
@@ -112,19 +111,3 @@ impl RngCore for SmallRng { | |
self.0.fill_bytes(dest) | ||
} | ||
} | ||
|
||
impl SmallRng { | ||
/// Construct an instance seeded from `rand::rng` | ||
/// | ||
/// # Panics | ||
/// | ||
/// This method panics only if [`crate::rng()`] fails to | ||
/// initialize. | ||
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] | ||
#[inline(always)] | ||
pub fn from_thread_rng() -> Self { | ||
let mut seed = <Rng as SeedableRng>::Seed::default(); | ||
crate::rng().fill_bytes(seed.as_mut()); | ||
SmallRng(Rng::from_seed(seed)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use of
from_os_rng
should not influence the benchmark results since only time for the closure passed toiter
is measured.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.
Agreed. It may improve set-up time slightly; at any rate I see no harm in using
rand::rng()
.