Skip to content

Add fn rand::make_rng#1734

Open
dhardy wants to merge 3 commits intomasterfrom
push-rtuwusonuukn
Open

Add fn rand::make_rng#1734
dhardy wants to merge 3 commits intomasterfrom
push-rtuwusonuukn

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 e79fcb2 to 7890243 Compare February 4, 2026 16:35
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.

2 participants