Skip to content

Conversation

@dhardy
Copy link
Member

@dhardy dhardy commented Feb 4, 2026

  • Added a CHANGELOG.md entry

Summary

Adds:

/// Construct and seed an RNG
///
/// This method yields a seeded RNG, using [`rng`] ([`ThreadRng`]) if enabled or
/// [`SysRng`] otherwise.
///
/// # Examples
///
/// ```
/// let mut rng: rand::rngs::SmallRng = rand::make_rng();
/// # let _ = rand::Rng::next_u32(&mut rng);
/// ```
///
/// # Security
///
/// Refer to [`ThreadRng#Security`].
#[cfg(feature = "sys_rng")]
pub fn make_rng<R: SeedableRng>() -> R { /* ... */ }

Motivation

We have removed SeedableRng::from_os_rng() since the last release. SomeRng::try_from_rng(&mut getrandom::SysRng).unwrap() is not exactly convenient to use.

We could put something like this under getrandom, but it feels more appropriate within rand (also allowing usage of ThreadRng).

Details

This is also slightly contentious, but I feel it's useful enough to be worth having. We could add trait SeedableRngExt instead, but that would have significantly more API surface.

@dhardy dhardy requested review from josephlr, newpavlov and vks February 4, 2026 12:30
@newpavlov
Copy link
Member

newpavlov commented Feb 4, 2026

Wouldn't it be better to introduce RngExt::from_sys_rng() -> Self where Self: SeedableRng?

@dhardy
Copy link
Member Author

dhardy commented Feb 4, 2026

That would allow SomeRng::from_sys_rng(), but I prefer this since:

  • RngExt is a surprising place to put from_sys_rng
  • Using RngExt may require an extra import (<SomeRng as rand::RngExt>::from_sys_rng() is quite convoluted)

@dhardy dhardy force-pushed the push-rtuwusonuukn branch from 2ba057a to b275cb6 Compare February 7, 2026 17:33
@dhardy dhardy requested a review from mstoeckl February 7, 2026 17:33
Copy link

@mstoeckl mstoeckl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This looks like a relatively low impact change; I expect it will be hard to confuse with rand::rng (since that returns the non-seedable ThreadRng; and the documentation is clear in both cases.)

One possible issue is that SysRng isn't infallible, so this can panic... (but so can ThreadRng, on first use.)

@dhardy dhardy merged commit 95c5165 into master Feb 8, 2026
16 checks passed
@dhardy dhardy deleted the push-rtuwusonuukn branch February 8, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants