Coverage Report

Created: 2025-11-16 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/fastrand-2.3.0/src/global_rng.rs
Line
Count
Source
1
//! A global, thread-local random number generator.
2
3
use crate::Rng;
4
5
use std::cell::Cell;
6
use std::ops::RangeBounds;
7
use std::vec::Vec;
8
9
// Chosen by fair roll of the dice.
10
const DEFAULT_RNG_SEED: u64 = 0xef6f79ed30ba75a;
11
12
impl Default for Rng {
13
    /// Initialize the `Rng` from the system's random number generator.
14
    ///
15
    /// This is equivalent to [`Rng::new()`].
16
    #[inline]
17
0
    fn default() -> Rng {
18
0
        Rng::new()
19
0
    }
20
}
21
22
impl Rng {
23
    /// Creates a new random number generator.
24
    #[inline]
25
0
    pub fn new() -> Rng {
26
0
        try_with_rng(Rng::fork).unwrap_or_else(|_| Rng::with_seed(0x4d595df4d0f33173))
Unexecuted instantiation: <fastrand::Rng>::new::{closure#0}
Unexecuted instantiation: <fastrand::Rng>::new::{closure#0}
27
0
    }
Unexecuted instantiation: <fastrand::Rng>::new
Unexecuted instantiation: <fastrand::Rng>::new
28
}
29
30
std::thread_local! {
31
    static RNG: Cell<Rng> = Cell::new(Rng(random_seed().unwrap_or(DEFAULT_RNG_SEED)));
32
}
33
34
/// Run an operation with the current thread-local generator.
35
#[inline]
36
0
fn with_rng<R>(f: impl FnOnce(&mut Rng) -> R) -> R {
37
0
    RNG.with(|rng| {
38
0
        let current = rng.replace(Rng(0));
39
40
0
        let mut restore = RestoreOnDrop { rng, current };
41
42
0
        f(&mut restore.current)
43
0
    })
Unexecuted instantiation: fastrand::global_rng::with_rng::<f64, fastrand::global_rng::f64::{closure#0}>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::with_rng::<f32, fastrand::global_rng::f32::{closure#0}>::{closure#0}
44
0
}
Unexecuted instantiation: fastrand::global_rng::with_rng::<f64, fastrand::global_rng::f64::{closure#0}>
Unexecuted instantiation: fastrand::global_rng::with_rng::<f32, fastrand::global_rng::f32::{closure#0}>
45
46
/// Try to run an operation with the current thread-local generator.
47
#[inline]
48
0
fn try_with_rng<R>(f: impl FnOnce(&mut Rng) -> R) -> Result<R, std::thread::AccessError> {
49
0
    RNG.try_with(|rng| {
50
0
        let current = rng.replace(Rng(0));
51
52
0
        let mut restore = RestoreOnDrop { rng, current };
53
54
0
        f(&mut restore.current)
55
0
    })
Unexecuted instantiation: fastrand::global_rng::try_with_rng::<fastrand::Rng, <fastrand::Rng>::fork>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::try_with_rng::<_, _>::{closure#0}
56
0
}
Unexecuted instantiation: fastrand::global_rng::try_with_rng::<fastrand::Rng, <fastrand::Rng>::fork>
Unexecuted instantiation: fastrand::global_rng::try_with_rng::<_, _>
57
58
/// Make sure the original RNG is restored even on panic.
59
struct RestoreOnDrop<'a> {
60
    rng: &'a Cell<Rng>,
61
    current: Rng,
62
}
63
64
impl Drop for RestoreOnDrop<'_> {
65
0
    fn drop(&mut self) {
66
0
        self.rng.set(Rng(self.current.0));
67
0
    }
68
}
69
70
/// Initializes the thread-local generator with the given seed.
71
#[inline]
72
0
pub fn seed(seed: u64) {
73
0
    with_rng(|r| r.seed(seed));
74
0
}
75
76
/// Gives back **current** seed that is being held by the thread-local generator.
77
#[inline]
78
0
pub fn get_seed() -> u64 {
79
0
    with_rng(|r| r.get_seed())
80
0
}
81
82
/// Generates a random `bool`.
83
#[inline]
84
0
pub fn bool() -> bool {
85
0
    with_rng(|r| r.bool())
86
0
}
87
88
/// Generates a random `char` in ranges a-z and A-Z.
89
#[inline]
90
0
pub fn alphabetic() -> char {
91
0
    with_rng(|r| r.alphabetic())
92
0
}
93
94
/// Generates a random `char` in ranges a-z, A-Z and 0-9.
95
#[inline]
96
0
pub fn alphanumeric() -> char {
97
0
    with_rng(|r| r.alphanumeric())
98
0
}
99
100
/// Generates a random `char` in range a-z.
101
#[inline]
102
0
pub fn lowercase() -> char {
103
0
    with_rng(|r| r.lowercase())
104
0
}
105
106
/// Generates a random `char` in range A-Z.
107
#[inline]
108
0
pub fn uppercase() -> char {
109
0
    with_rng(|r| r.uppercase())
110
0
}
111
112
/// Choose an item from an iterator at random.
113
///
114
/// This function may have an unexpected result if the `len()` property of the
115
/// iterator does not match the actual number of items in the iterator. If
116
/// the iterator is empty, this returns `None`.
117
#[inline]
118
0
pub fn choice<I>(iter: I) -> Option<I::Item>
119
0
where
120
0
    I: IntoIterator,
121
0
    I::IntoIter: ExactSizeIterator,
122
{
123
0
    with_rng(|r| r.choice(iter))
124
0
}
125
126
/// Generates a random digit in the given `base`.
127
///
128
/// Digits are represented by `char`s in ranges 0-9 and a-z.
129
///
130
/// Panics if the base is zero or greater than 36.
131
#[inline]
132
0
pub fn digit(base: u32) -> char {
133
0
    with_rng(|r| r.digit(base))
134
0
}
135
136
/// Shuffles a slice randomly.
137
#[inline]
138
0
pub fn shuffle<T>(slice: &mut [T]) {
139
0
    with_rng(|r| r.shuffle(slice))
140
0
}
141
142
/// Fill a byte slice with random data.
143
#[inline]
144
0
pub fn fill(slice: &mut [u8]) {
145
0
    with_rng(|r| r.fill(slice))
146
0
}
147
148
macro_rules! integer {
149
    ($t:tt, $doc:tt) => {
150
        #[doc = $doc]
151
        ///
152
        /// Panics if the range is empty.
153
        #[inline]
154
0
        pub fn $t(range: impl RangeBounds<$t>) -> $t {
155
0
            with_rng(|r| r.$t(range))
Unexecuted instantiation: fastrand::global_rng::i8::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::u8::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::i16::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::i32::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::i64::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::u16::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::u32::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::u64::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::char::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::i128::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::u128::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::isize::<_>::{closure#0}
Unexecuted instantiation: fastrand::global_rng::usize::<_>::{closure#0}
156
0
        }
Unexecuted instantiation: fastrand::global_rng::i8::<_>
Unexecuted instantiation: fastrand::global_rng::u8::<_>
Unexecuted instantiation: fastrand::global_rng::i16::<_>
Unexecuted instantiation: fastrand::global_rng::i32::<_>
Unexecuted instantiation: fastrand::global_rng::i64::<_>
Unexecuted instantiation: fastrand::global_rng::u16::<_>
Unexecuted instantiation: fastrand::global_rng::u32::<_>
Unexecuted instantiation: fastrand::global_rng::u64::<_>
Unexecuted instantiation: fastrand::global_rng::char::<_>
Unexecuted instantiation: fastrand::global_rng::i128::<_>
Unexecuted instantiation: fastrand::global_rng::u128::<_>
Unexecuted instantiation: fastrand::global_rng::isize::<_>
Unexecuted instantiation: fastrand::global_rng::usize::<_>
157
    };
158
}
159
160
integer!(u8, "Generates a random `u8` in the given range.");
161
integer!(i8, "Generates a random `i8` in the given range.");
162
integer!(u16, "Generates a random `u16` in the given range.");
163
integer!(i16, "Generates a random `i16` in the given range.");
164
integer!(u32, "Generates a random `u32` in the given range.");
165
integer!(i32, "Generates a random `i32` in the given range.");
166
integer!(u64, "Generates a random `u64` in the given range.");
167
integer!(i64, "Generates a random `i64` in the given range.");
168
integer!(u128, "Generates a random `u128` in the given range.");
169
integer!(i128, "Generates a random `i128` in the given range.");
170
integer!(usize, "Generates a random `usize` in the given range.");
171
integer!(isize, "Generates a random `isize` in the given range.");
172
integer!(char, "Generates a random `char` in the given range.");
173
174
/// Generates a random `f32` in range `0..1`.
175
0
pub fn f32() -> f32 {
176
0
    with_rng(|r| r.f32())
177
0
}
178
179
/// Generates a random `f64` in range `0..1`.
180
0
pub fn f64() -> f64 {
181
0
    with_rng(|r| r.f64())
182
0
}
183
184
/// Collects `amount` values at random from the iterable into a vector.
185
0
pub fn choose_multiple<I: IntoIterator>(source: I, amount: usize) -> Vec<I::Item> {
186
0
    with_rng(|rng| rng.choose_multiple(source, amount))
187
0
}
188
189
#[cfg(not(all(
190
    any(target_arch = "wasm32", target_arch = "wasm64"),
191
    target_os = "unknown"
192
)))]
193
0
fn random_seed() -> Option<u64> {
194
    use std::collections::hash_map::DefaultHasher;
195
    use std::hash::{Hash, Hasher};
196
    use std::thread;
197
    use std::time::Instant;
198
199
0
    let mut hasher = DefaultHasher::new();
200
0
    Instant::now().hash(&mut hasher);
201
0
    thread::current().id().hash(&mut hasher);
202
0
    Some(hasher.finish())
203
0
}
204
205
#[cfg(all(
206
    any(target_arch = "wasm32", target_arch = "wasm64"),
207
    target_os = "unknown",
208
    feature = "js"
209
))]
210
fn random_seed() -> Option<u64> {
211
    // TODO(notgull): Failures should be logged somewhere.
212
    let mut seed = [0u8; 8];
213
    getrandom::getrandom(&mut seed).ok()?;
214
    Some(u64::from_ne_bytes(seed))
215
}
216
217
#[cfg(all(
218
    any(target_arch = "wasm32", target_arch = "wasm64"),
219
    target_os = "unknown",
220
    not(feature = "js")
221
))]
222
fn random_seed() -> Option<u64> {
223
    None
224
}