grain_id/rand.rs
1use super::*;
2use ::rand::{
3 Rng, RngExt as _,
4 distr::{Distribution, StandardUniform},
5};
6
7impl Distribution<GrainId> for StandardUniform {
8 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> GrainId {
9 <GrainId>::from_u64_lossy(rng.random())
10 }
11}
12impl GrainId {
13 /// Generate a new random [`GrainId`].
14 ///
15 /// This method generate a random ID.
16 /// The generated ID is guaranteed to not be the [`NIL`](Self::NIL) value.
17 ///
18 /// # Examples
19 ///
20 /// ```
21 /// # use grain_id::*;
22 /// let id = GrainId::random();
23 /// assert_ne!(id, GrainId::NIL);
24 /// ```
25 pub fn random() -> Self {
26 <GrainId>::from_u64_lossy(::rand::random())
27 }
28}