/rust/registry/src/index.crates.io-6f17d22bba15001f/fastrand-2.3.0/src/lib.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! A simple and fast random number generator. |
2 | | //! |
3 | | //! The implementation uses [Wyrand](https://github.com/wangyi-fudan/wyhash), a simple and fast |
4 | | //! generator but **not** cryptographically secure. |
5 | | //! |
6 | | //! # Examples |
7 | | //! |
8 | | //! Flip a coin: |
9 | | //! |
10 | | //! ``` |
11 | | //! if fastrand::bool() { |
12 | | //! println!("heads"); |
13 | | //! } else { |
14 | | //! println!("tails"); |
15 | | //! } |
16 | | //! ``` |
17 | | //! |
18 | | //! Generate a random `i32`: |
19 | | //! |
20 | | //! ``` |
21 | | //! let num = fastrand::i32(..); |
22 | | //! ``` |
23 | | //! |
24 | | //! Choose a random element in an array: |
25 | | //! |
26 | | //! ``` |
27 | | //! let v = vec![1, 2, 3, 4, 5]; |
28 | | //! let i = fastrand::usize(..v.len()); |
29 | | //! let elem = v[i]; |
30 | | //! ``` |
31 | | //! |
32 | | //! Sample values from an array with `O(n)` complexity (`n` is the length of array): |
33 | | //! |
34 | | //! ``` |
35 | | //! fastrand::choose_multiple([1, 4, 5], 2); |
36 | | //! fastrand::choose_multiple(0..20, 12); |
37 | | //! ``` |
38 | | //! |
39 | | //! |
40 | | //! Shuffle an array: |
41 | | //! |
42 | | //! ``` |
43 | | //! let mut v = vec![1, 2, 3, 4, 5]; |
44 | | //! fastrand::shuffle(&mut v); |
45 | | //! ``` |
46 | | //! |
47 | | //! Generate a random [`Vec`] or [`String`]: |
48 | | //! |
49 | | //! ``` |
50 | | //! use std::iter::repeat_with; |
51 | | //! |
52 | | //! let v: Vec<i32> = repeat_with(|| fastrand::i32(..)).take(10).collect(); |
53 | | //! let s: String = repeat_with(fastrand::alphanumeric).take(10).collect(); |
54 | | //! ``` |
55 | | //! |
56 | | //! To get reproducible results on every run, initialize the generator with a seed: |
57 | | //! |
58 | | //! ``` |
59 | | //! // Pick an arbitrary number as seed. |
60 | | //! fastrand::seed(7); |
61 | | //! |
62 | | //! // Now this prints the same number on every run: |
63 | | //! println!("{}", fastrand::u32(..)); |
64 | | //! ``` |
65 | | //! |
66 | | //! To be more efficient, create a new [`Rng`] instance instead of using the thread-local |
67 | | //! generator: |
68 | | //! |
69 | | //! ``` |
70 | | //! use std::iter::repeat_with; |
71 | | //! |
72 | | //! let mut rng = fastrand::Rng::new(); |
73 | | //! let mut bytes: Vec<u8> = repeat_with(|| rng.u8(..)).take(10_000).collect(); |
74 | | //! ``` |
75 | | //! |
76 | | //! This crate aims to expose a core set of useful randomness primitives. For more niche algorithms, |
77 | | //! consider using the [`fastrand-contrib`] crate alongside this one. |
78 | | //! |
79 | | //! # Features |
80 | | //! |
81 | | //! - `std` (enabled by default): Enables the `std` library. This is required for the global |
82 | | //! generator and global entropy. Without this feature, [`Rng`] can only be instantiated using |
83 | | //! the [`with_seed`](Rng::with_seed) method. |
84 | | //! - `js`: Assumes that WebAssembly targets are being run in a JavaScript environment. See the |
85 | | //! [WebAssembly Notes](#webassembly-notes) section for more information. |
86 | | //! |
87 | | //! # WebAssembly Notes |
88 | | //! |
89 | | //! For non-WASI WASM targets, there is additional sublety to consider when utilizing the global RNG. |
90 | | //! By default, `std` targets will use entropy sources in the standard library to seed the global RNG. |
91 | | //! However, these sources are not available by default on WASM targets outside of WASI. |
92 | | //! |
93 | | //! If the `js` feature is enabled, this crate will assume that it is running in a JavaScript |
94 | | //! environment. At this point, the [`getrandom`] crate will be used in order to access the available |
95 | | //! entropy sources and seed the global RNG. If the `js` feature is not enabled, the global RNG will |
96 | | //! use a predefined seed. |
97 | | //! |
98 | | //! [`fastrand-contrib`]: https://crates.io/crates/fastrand-contrib |
99 | | //! [`getrandom`]: https://crates.io/crates/getrandom |
100 | | |
101 | | #![no_std] |
102 | | #![cfg_attr(docsrs, feature(doc_cfg))] |
103 | | #![forbid(unsafe_code)] |
104 | | #![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)] |
105 | | #![doc( |
106 | | html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png" |
107 | | )] |
108 | | #![doc( |
109 | | html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png" |
110 | | )] |
111 | | |
112 | | #[cfg(feature = "alloc")] |
113 | | extern crate alloc; |
114 | | #[cfg(feature = "std")] |
115 | | extern crate std; |
116 | | |
117 | | use core::convert::{TryFrom, TryInto}; |
118 | | use core::ops::{Bound, RangeBounds}; |
119 | | |
120 | | #[cfg(feature = "alloc")] |
121 | | use alloc::vec::Vec; |
122 | | |
123 | | #[cfg(feature = "std")] |
124 | | #[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
125 | | mod global_rng; |
126 | | |
127 | | #[cfg(feature = "std")] |
128 | | pub use global_rng::*; |
129 | | |
130 | | /// A random number generator. |
131 | | #[derive(Debug, PartialEq, Eq)] |
132 | | pub struct Rng(u64); |
133 | | |
134 | | impl Clone for Rng { |
135 | | /// Clones the generator by creating a new generator with the same seed. |
136 | 0 | fn clone(&self) -> Rng { |
137 | 0 | Rng::with_seed(self.0) |
138 | 0 | } |
139 | | } |
140 | | |
141 | | impl Rng { |
142 | | /// Generates a random `u32`. |
143 | | #[inline] |
144 | 0 | fn gen_u32(&mut self) -> u32 { |
145 | 0 | self.gen_u64() as u32 |
146 | 0 | } |
147 | | |
148 | | /// Generates a random `u64`. |
149 | | #[inline] |
150 | 51.7k | fn gen_u64(&mut self) -> u64 { |
151 | | // Constants for WyRand taken from: https://github.com/wangyi-fudan/wyhash/blob/master/wyhash.h#L151 |
152 | | // Updated for the final v4.2 implementation with improved constants for better entropy output. |
153 | | const WY_CONST_0: u64 = 0x2d35_8dcc_aa6c_78a5; |
154 | | const WY_CONST_1: u64 = 0x8bb8_4b93_962e_acc9; |
155 | | |
156 | 51.7k | let s = self.0.wrapping_add(WY_CONST_0); |
157 | 51.7k | self.0 = s; |
158 | 51.7k | let t = u128::from(s) * u128::from(s ^ WY_CONST_1); |
159 | 51.7k | (t as u64) ^ (t >> 64) as u64 |
160 | 51.7k | } Line | Count | Source | 150 | 51.7k | fn gen_u64(&mut self) -> u64 { | 151 | | // Constants for WyRand taken from: https://github.com/wangyi-fudan/wyhash/blob/master/wyhash.h#L151 | 152 | | // Updated for the final v4.2 implementation with improved constants for better entropy output. | 153 | | const WY_CONST_0: u64 = 0x2d35_8dcc_aa6c_78a5; | 154 | | const WY_CONST_1: u64 = 0x8bb8_4b93_962e_acc9; | 155 | | | 156 | 51.7k | let s = self.0.wrapping_add(WY_CONST_0); | 157 | 51.7k | self.0 = s; | 158 | 51.7k | let t = u128::from(s) * u128::from(s ^ WY_CONST_1); | 159 | 51.7k | (t as u64) ^ (t >> 64) as u64 | 160 | 51.7k | } |
Unexecuted instantiation: <fastrand::Rng>::gen_u64 |
161 | | |
162 | | /// Generates a random `u128`. |
163 | | #[inline] |
164 | 0 | fn gen_u128(&mut self) -> u128 { |
165 | 0 | (u128::from(self.gen_u64()) << 64) | u128::from(self.gen_u64()) |
166 | 0 | } |
167 | | |
168 | | /// Generates a random `u32` in `0..n`. |
169 | | #[inline] |
170 | 0 | fn gen_mod_u32(&mut self, n: u32) -> u32 { |
171 | 0 | // Adapted from: https://lemire.me/blog/2016/06/30/fast-random-shuffling/ |
172 | 0 | let mut r = self.gen_u32(); |
173 | 0 | let mut hi = mul_high_u32(r, n); |
174 | 0 | let mut lo = r.wrapping_mul(n); |
175 | 0 | if lo < n { |
176 | 0 | let t = n.wrapping_neg() % n; |
177 | 0 | while lo < t { |
178 | 0 | r = self.gen_u32(); |
179 | 0 | hi = mul_high_u32(r, n); |
180 | 0 | lo = r.wrapping_mul(n); |
181 | 0 | } |
182 | 0 | } |
183 | 0 | hi |
184 | 0 | } |
185 | | |
186 | | /// Generates a random `u64` in `0..n`. |
187 | | #[inline] |
188 | 44.3k | fn gen_mod_u64(&mut self, n: u64) -> u64 { |
189 | 44.3k | // Adapted from: https://lemire.me/blog/2016/06/30/fast-random-shuffling/ |
190 | 44.3k | let mut r = self.gen_u64(); |
191 | 44.3k | let mut hi = mul_high_u64(r, n); |
192 | 44.3k | let mut lo = r.wrapping_mul(n); |
193 | 44.3k | if lo < n { |
194 | 0 | let t = n.wrapping_neg() % n; |
195 | 0 | while lo < t { |
196 | 0 | r = self.gen_u64(); |
197 | 0 | hi = mul_high_u64(r, n); |
198 | 0 | lo = r.wrapping_mul(n); |
199 | 0 | } |
200 | 44.3k | } |
201 | 44.3k | hi |
202 | 44.3k | } <fastrand::Rng>::gen_mod_u64 Line | Count | Source | 188 | 44.3k | fn gen_mod_u64(&mut self, n: u64) -> u64 { | 189 | 44.3k | // Adapted from: https://lemire.me/blog/2016/06/30/fast-random-shuffling/ | 190 | 44.3k | let mut r = self.gen_u64(); | 191 | 44.3k | let mut hi = mul_high_u64(r, n); | 192 | 44.3k | let mut lo = r.wrapping_mul(n); | 193 | 44.3k | if lo < n { | 194 | 0 | let t = n.wrapping_neg() % n; | 195 | 0 | while lo < t { | 196 | 0 | r = self.gen_u64(); | 197 | 0 | hi = mul_high_u64(r, n); | 198 | 0 | lo = r.wrapping_mul(n); | 199 | 0 | } | 200 | 44.3k | } | 201 | 44.3k | hi | 202 | 44.3k | } |
Unexecuted instantiation: <fastrand::Rng>::gen_mod_u64 |
203 | | |
204 | | /// Generates a random `u128` in `0..n`. |
205 | | #[inline] |
206 | 0 | fn gen_mod_u128(&mut self, n: u128) -> u128 { |
207 | 0 | // Adapted from: https://lemire.me/blog/2016/06/30/fast-random-shuffling/ |
208 | 0 | let mut r = self.gen_u128(); |
209 | 0 | let mut hi = mul_high_u128(r, n); |
210 | 0 | let mut lo = r.wrapping_mul(n); |
211 | 0 | if lo < n { |
212 | 0 | let t = n.wrapping_neg() % n; |
213 | 0 | while lo < t { |
214 | 0 | r = self.gen_u128(); |
215 | 0 | hi = mul_high_u128(r, n); |
216 | 0 | lo = r.wrapping_mul(n); |
217 | 0 | } |
218 | 0 | } |
219 | 0 | hi |
220 | 0 | } |
221 | | } |
222 | | |
223 | | /// Computes `(a * b) >> 32`. |
224 | | #[inline] |
225 | 0 | fn mul_high_u32(a: u32, b: u32) -> u32 { |
226 | 0 | (((a as u64) * (b as u64)) >> 32) as u32 |
227 | 0 | } |
228 | | |
229 | | /// Computes `(a * b) >> 64`. |
230 | | #[inline] |
231 | 44.3k | fn mul_high_u64(a: u64, b: u64) -> u64 { |
232 | 44.3k | (((a as u128) * (b as u128)) >> 64) as u64 |
233 | 44.3k | } Line | Count | Source | 231 | 44.3k | fn mul_high_u64(a: u64, b: u64) -> u64 { | 232 | 44.3k | (((a as u128) * (b as u128)) >> 64) as u64 | 233 | 44.3k | } |
Unexecuted instantiation: fastrand::mul_high_u64 |
234 | | |
235 | | /// Computes `(a * b) >> 128`. |
236 | | #[inline] |
237 | 0 | fn mul_high_u128(a: u128, b: u128) -> u128 { |
238 | 0 | // Adapted from: https://stackoverflow.com/a/28904636 |
239 | 0 | let a_lo = a as u64 as u128; |
240 | 0 | let a_hi = (a >> 64) as u64 as u128; |
241 | 0 | let b_lo = b as u64 as u128; |
242 | 0 | let b_hi = (b >> 64) as u64 as u128; |
243 | 0 | let carry = (a_lo * b_lo) >> 64; |
244 | 0 | let carry = ((a_hi * b_lo) as u64 as u128 + (a_lo * b_hi) as u64 as u128 + carry) >> 64; |
245 | 0 | a_hi * b_hi + ((a_hi * b_lo) >> 64) + ((a_lo * b_hi) >> 64) + carry |
246 | 0 | } |
247 | | |
248 | | macro_rules! rng_integer { |
249 | | ($t:tt, $unsigned_t:tt, $gen:tt, $mod:tt, $doc:tt) => { |
250 | | #[doc = $doc] |
251 | | /// |
252 | | /// Panics if the range is empty. |
253 | | #[inline] |
254 | 44.3k | pub fn $t(&mut self, range: impl RangeBounds<$t>) -> $t { |
255 | 44.3k | let panic_empty_range = || { |
256 | 0 | panic!( |
257 | 0 | "empty range: {:?}..{:?}", |
258 | 0 | range.start_bound(), |
259 | 0 | range.end_bound() |
260 | 0 | ) Unexecuted instantiation: <fastrand::Rng>::usize::<core::ops::range::Range<usize>>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::u32::<core::ops::range::RangeFull>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::u64::<core::ops::range::RangeFull>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::i8::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::i16::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::i32::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::i64::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::i128::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::isize::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::u8::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::u16::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::u128::<_>::{closure#0} Unexecuted instantiation: <fastrand::Rng>::usize::<_>::{closure#0} |
261 | | }; |
262 | | |
263 | 44.3k | let low = match range.start_bound() { |
264 | 0 | Bound::Unbounded => core::$t::MIN, |
265 | 44.3k | Bound::Included(&x) => x, |
266 | 0 | Bound::Excluded(&x) => x.checked_add(1).unwrap_or_else(panic_empty_range), |
267 | | }; |
268 | | |
269 | 44.3k | let high = match range.end_bound() { |
270 | 0 | Bound::Unbounded => core::$t::MAX, |
271 | 0 | Bound::Included(&x) => x, |
272 | 44.3k | Bound::Excluded(&x) => x.checked_sub(1).unwrap_or_else(panic_empty_range), |
273 | | }; |
274 | | |
275 | 44.3k | if low > high { |
276 | 0 | panic_empty_range(); |
277 | 44.3k | } |
278 | | |
279 | 44.3k | if low == core::$t::MIN && high == core::$t::MAX { |
280 | 0 | self.$gen() as $t |
281 | | } else { |
282 | 44.3k | let len = high.wrapping_sub(low).wrapping_add(1); |
283 | 44.3k | low.wrapping_add(self.$mod(len as $unsigned_t as _) as $t) |
284 | | } |
285 | 44.3k | } <fastrand::Rng>::usize::<core::ops::range::Range<usize>> Line | Count | Source | 254 | 44.3k | pub fn $t(&mut self, range: impl RangeBounds<$t>) -> $t { | 255 | 44.3k | let panic_empty_range = || { | 256 | | panic!( | 257 | | "empty range: {:?}..{:?}", | 258 | | range.start_bound(), | 259 | | range.end_bound() | 260 | | ) | 261 | | }; | 262 | | | 263 | 44.3k | let low = match range.start_bound() { | 264 | 0 | Bound::Unbounded => core::$t::MIN, | 265 | 44.3k | Bound::Included(&x) => x, | 266 | 0 | Bound::Excluded(&x) => x.checked_add(1).unwrap_or_else(panic_empty_range), | 267 | | }; | 268 | | | 269 | 44.3k | let high = match range.end_bound() { | 270 | 0 | Bound::Unbounded => core::$t::MAX, | 271 | 0 | Bound::Included(&x) => x, | 272 | 44.3k | Bound::Excluded(&x) => x.checked_sub(1).unwrap_or_else(panic_empty_range), | 273 | | }; | 274 | | | 275 | 44.3k | if low > high { | 276 | 0 | panic_empty_range(); | 277 | 44.3k | } | 278 | | | 279 | 44.3k | if low == core::$t::MIN && high == core::$t::MAX { | 280 | 0 | self.$gen() as $t | 281 | | } else { | 282 | 44.3k | let len = high.wrapping_sub(low).wrapping_add(1); | 283 | 44.3k | low.wrapping_add(self.$mod(len as $unsigned_t as _) as $t) | 284 | | } | 285 | 44.3k | } |
Unexecuted instantiation: <fastrand::Rng>::u32::<core::ops::range::RangeFull> Unexecuted instantiation: <fastrand::Rng>::u64::<core::ops::range::RangeFull> Unexecuted instantiation: <fastrand::Rng>::i8::<_> Unexecuted instantiation: <fastrand::Rng>::i16::<_> Unexecuted instantiation: <fastrand::Rng>::i32::<_> Unexecuted instantiation: <fastrand::Rng>::i64::<_> Unexecuted instantiation: <fastrand::Rng>::i128::<_> Unexecuted instantiation: <fastrand::Rng>::isize::<_> Unexecuted instantiation: <fastrand::Rng>::u8::<_> Unexecuted instantiation: <fastrand::Rng>::u16::<_> Unexecuted instantiation: <fastrand::Rng>::u128::<_> Unexecuted instantiation: <fastrand::Rng>::usize::<_> |
286 | | }; |
287 | | } |
288 | | |
289 | | impl Rng { |
290 | | /// Creates a new random number generator with the initial seed. |
291 | | #[inline] |
292 | | #[must_use = "this creates a new instance of `Rng`; if you want to initialize the thread-local generator, use `fastrand::seed()` instead"] |
293 | 7.39k | pub fn with_seed(seed: u64) -> Self { |
294 | 7.39k | Rng(seed) |
295 | 7.39k | } <fastrand::Rng>::with_seed Line | Count | Source | 293 | 7.39k | pub fn with_seed(seed: u64) -> Self { | 294 | 7.39k | Rng(seed) | 295 | 7.39k | } |
Unexecuted instantiation: <fastrand::Rng>::with_seed |
296 | | |
297 | | /// Clones the generator by deterministically deriving a new generator based on the initial |
298 | | /// seed. |
299 | | /// |
300 | | /// This function can be used to create a new generator that is a "spinoff" of the old |
301 | | /// generator. The new generator will not produce the same sequence of values as the |
302 | | /// old generator. |
303 | | /// |
304 | | /// # Example |
305 | | /// |
306 | | /// ``` |
307 | | /// // Seed two generators equally, and clone both of them. |
308 | | /// let mut base1 = fastrand::Rng::with_seed(0x4d595df4d0f33173); |
309 | | /// base1.bool(); // Use the generator once. |
310 | | /// |
311 | | /// let mut base2 = fastrand::Rng::with_seed(0x4d595df4d0f33173); |
312 | | /// base2.bool(); // Use the generator once. |
313 | | /// |
314 | | /// let mut rng1 = base1.fork(); |
315 | | /// let mut rng2 = base2.fork(); |
316 | | /// |
317 | | /// println!("rng1 returns {}", rng1.u32(..)); |
318 | | /// println!("rng2 returns {}", rng2.u32(..)); |
319 | | /// ``` |
320 | | #[inline] |
321 | | #[must_use = "this creates a new instance of `Rng`"] |
322 | 7.39k | pub fn fork(&mut self) -> Self { |
323 | 7.39k | Rng::with_seed(self.gen_u64()) |
324 | 7.39k | } Line | Count | Source | 322 | 7.39k | pub fn fork(&mut self) -> Self { | 323 | 7.39k | Rng::with_seed(self.gen_u64()) | 324 | 7.39k | } |
Unexecuted instantiation: <fastrand::Rng>::fork |
325 | | |
326 | | /// Generates a random `char` in ranges a-z and A-Z. |
327 | | #[inline] |
328 | 0 | pub fn alphabetic(&mut self) -> char { |
329 | | const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
330 | 0 | *self.choice(CHARS).unwrap() as char |
331 | 0 | } |
332 | | |
333 | | /// Generates a random `char` in ranges a-z, A-Z and 0-9. |
334 | | #[inline] |
335 | 44.3k | pub fn alphanumeric(&mut self) -> char { |
336 | | const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
337 | 44.3k | *self.choice(CHARS).unwrap() as char |
338 | 44.3k | } <fastrand::Rng>::alphanumeric Line | Count | Source | 335 | 44.3k | pub fn alphanumeric(&mut self) -> char { | 336 | | const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | 337 | 44.3k | *self.choice(CHARS).unwrap() as char | 338 | 44.3k | } |
Unexecuted instantiation: <fastrand::Rng>::alphanumeric |
339 | | |
340 | | /// Generates a random `bool`. |
341 | | #[inline] |
342 | 0 | pub fn bool(&mut self) -> bool { |
343 | 0 | self.u8(..) % 2 == 0 |
344 | 0 | } |
345 | | |
346 | | /// Generates a random digit in the given `base`. |
347 | | /// |
348 | | /// Digits are represented by `char`s in ranges 0-9 and a-z. |
349 | | /// |
350 | | /// Panics if the base is zero or greater than 36. |
351 | | #[inline] |
352 | 0 | pub fn digit(&mut self, base: u32) -> char { |
353 | 0 | if base == 0 { |
354 | 0 | panic!("base cannot be zero"); |
355 | 0 | } |
356 | 0 | if base > 36 { |
357 | 0 | panic!("base cannot be larger than 36"); |
358 | 0 | } |
359 | 0 | let num = self.u8(..base as u8); |
360 | 0 | if num < 10 { |
361 | 0 | (b'0' + num) as char |
362 | | } else { |
363 | 0 | (b'a' + num - 10) as char |
364 | | } |
365 | 0 | } |
366 | | |
367 | | /// Generates a random `f32` in range `0..1`. |
368 | 0 | pub fn f32(&mut self) -> f32 { |
369 | 0 | let b = 32; |
370 | 0 | let f = core::f32::MANTISSA_DIGITS - 1; |
371 | 0 | f32::from_bits((1 << (b - 2)) - (1 << f) + (self.u32(..) >> (b - f))) - 1.0 |
372 | 0 | } |
373 | | |
374 | | /// Generates a random `f64` in range `0..1`. |
375 | 0 | pub fn f64(&mut self) -> f64 { |
376 | 0 | let b = 64; |
377 | 0 | let f = core::f64::MANTISSA_DIGITS - 1; |
378 | 0 | f64::from_bits((1 << (b - 2)) - (1 << f) + (self.u64(..) >> (b - f))) - 1.0 |
379 | 0 | } |
380 | | |
381 | | /// Collects `amount` values at random from the iterable into a vector. |
382 | | /// |
383 | | /// The length of the returned vector equals `amount` unless the iterable |
384 | | /// contains insufficient elements, in which case it equals the number of |
385 | | /// elements available. |
386 | | /// |
387 | | /// Complexity is `O(n)` where `n` is the length of the iterable. |
388 | | #[cfg(feature = "alloc")] |
389 | | #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] |
390 | 0 | pub fn choose_multiple<I: IntoIterator>(&mut self, source: I, amount: usize) -> Vec<I::Item> { |
391 | 0 | // Adapted from: https://docs.rs/rand/latest/rand/seq/trait.IteratorRandom.html#method.choose_multiple |
392 | 0 | let mut reservoir = Vec::with_capacity(amount); |
393 | 0 | let mut iter = source.into_iter(); |
394 | 0 |
|
395 | 0 | reservoir.extend(iter.by_ref().take(amount)); |
396 | 0 |
|
397 | 0 | // Continue unless the iterator was exhausted |
398 | 0 | // |
399 | 0 | // note: this prevents iterators that "restart" from causing problems. |
400 | 0 | // If the iterator stops once, then so do we. |
401 | 0 | if reservoir.len() == amount { |
402 | 0 | for (i, elem) in iter.enumerate() { |
403 | 0 | let end = i + 1 + amount; |
404 | 0 | let k = self.usize(0..end); |
405 | 0 | if let Some(slot) = reservoir.get_mut(k) { |
406 | 0 | *slot = elem; |
407 | 0 | } |
408 | | } |
409 | | } else { |
410 | | // If less than one third of the `Vec` was used, reallocate |
411 | | // so that the unused space is not wasted. There is a corner |
412 | | // case where `amount` was much less than `self.len()`. |
413 | 0 | if reservoir.capacity() > 3 * reservoir.len() { |
414 | 0 | reservoir.shrink_to_fit(); |
415 | 0 | } |
416 | | } |
417 | 0 | reservoir |
418 | 0 | } |
419 | | |
420 | | rng_integer!( |
421 | | i8, |
422 | | u8, |
423 | | gen_u32, |
424 | | gen_mod_u32, |
425 | | "Generates a random `i8` in the given range." |
426 | | ); |
427 | | |
428 | | rng_integer!( |
429 | | i16, |
430 | | u16, |
431 | | gen_u32, |
432 | | gen_mod_u32, |
433 | | "Generates a random `i16` in the given range." |
434 | | ); |
435 | | |
436 | | rng_integer!( |
437 | | i32, |
438 | | u32, |
439 | | gen_u32, |
440 | | gen_mod_u32, |
441 | | "Generates a random `i32` in the given range." |
442 | | ); |
443 | | |
444 | | rng_integer!( |
445 | | i64, |
446 | | u64, |
447 | | gen_u64, |
448 | | gen_mod_u64, |
449 | | "Generates a random `i64` in the given range." |
450 | | ); |
451 | | |
452 | | rng_integer!( |
453 | | i128, |
454 | | u128, |
455 | | gen_u128, |
456 | | gen_mod_u128, |
457 | | "Generates a random `i128` in the given range." |
458 | | ); |
459 | | |
460 | | #[cfg(target_pointer_width = "16")] |
461 | | rng_integer!( |
462 | | isize, |
463 | | usize, |
464 | | gen_u32, |
465 | | gen_mod_u32, |
466 | | "Generates a random `isize` in the given range." |
467 | | ); |
468 | | #[cfg(target_pointer_width = "32")] |
469 | | rng_integer!( |
470 | | isize, |
471 | | usize, |
472 | | gen_u32, |
473 | | gen_mod_u32, |
474 | | "Generates a random `isize` in the given range." |
475 | | ); |
476 | | #[cfg(target_pointer_width = "64")] |
477 | | rng_integer!( |
478 | | isize, |
479 | | usize, |
480 | | gen_u64, |
481 | | gen_mod_u64, |
482 | | "Generates a random `isize` in the given range." |
483 | | ); |
484 | | |
485 | | /// Generates a random `char` in range a-z. |
486 | | #[inline] |
487 | 0 | pub fn lowercase(&mut self) -> char { |
488 | | const CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyz"; |
489 | 0 | *self.choice(CHARS).unwrap() as char |
490 | 0 | } |
491 | | |
492 | | /// Initializes this generator with the given seed. |
493 | | #[inline] |
494 | 0 | pub fn seed(&mut self, seed: u64) { |
495 | 0 | self.0 = seed; |
496 | 0 | } Unexecuted instantiation: <fastrand::Rng>::seed Unexecuted instantiation: <fastrand::Rng>::seed |
497 | | |
498 | | /// Gives back **current** seed that is being held by this generator. |
499 | | #[inline] |
500 | 0 | pub fn get_seed(&self) -> u64 { |
501 | 0 | self.0 |
502 | 0 | } |
503 | | |
504 | | /// Choose an item from an iterator at random. |
505 | | /// |
506 | | /// This function may have an unexpected result if the `len()` property of the |
507 | | /// iterator does not match the actual number of items in the iterator. If |
508 | | /// the iterator is empty, this returns `None`. |
509 | | #[inline] |
510 | 44.3k | pub fn choice<I>(&mut self, iter: I) -> Option<I::Item> |
511 | 44.3k | where |
512 | 44.3k | I: IntoIterator, |
513 | 44.3k | I::IntoIter: ExactSizeIterator, |
514 | 44.3k | { |
515 | 44.3k | let mut iter = iter.into_iter(); |
516 | 44.3k | |
517 | 44.3k | // Get the item at a random index. |
518 | 44.3k | let len = iter.len(); |
519 | 44.3k | if len == 0 { |
520 | 0 | return None; |
521 | 44.3k | } |
522 | 44.3k | let index = self.usize(0..len); |
523 | 44.3k | |
524 | 44.3k | iter.nth(index) |
525 | 44.3k | } <fastrand::Rng>::choice::<&[u8]> Line | Count | Source | 510 | 44.3k | pub fn choice<I>(&mut self, iter: I) -> Option<I::Item> | 511 | 44.3k | where | 512 | 44.3k | I: IntoIterator, | 513 | 44.3k | I::IntoIter: ExactSizeIterator, | 514 | 44.3k | { | 515 | 44.3k | let mut iter = iter.into_iter(); | 516 | 44.3k | | 517 | 44.3k | // Get the item at a random index. | 518 | 44.3k | let len = iter.len(); | 519 | 44.3k | if len == 0 { | 520 | 0 | return None; | 521 | 44.3k | } | 522 | 44.3k | let index = self.usize(0..len); | 523 | 44.3k | | 524 | 44.3k | iter.nth(index) | 525 | 44.3k | } |
Unexecuted instantiation: <fastrand::Rng>::choice::<_> |
526 | | |
527 | | /// Shuffles a slice randomly. |
528 | | #[inline] |
529 | 0 | pub fn shuffle<T>(&mut self, slice: &mut [T]) { |
530 | 0 | for i in 1..slice.len() { |
531 | 0 | slice.swap(i, self.usize(..=i)); |
532 | 0 | } |
533 | 0 | } |
534 | | |
535 | | /// Fill a byte slice with random data. |
536 | | #[inline] |
537 | 0 | pub fn fill(&mut self, slice: &mut [u8]) { |
538 | 0 | // We fill the slice by chunks of 8 bytes, or one block of |
539 | 0 | // WyRand output per new state. |
540 | 0 | let mut chunks = slice.chunks_exact_mut(core::mem::size_of::<u64>()); |
541 | 0 | for chunk in chunks.by_ref() { |
542 | 0 | let n = self.gen_u64().to_ne_bytes(); |
543 | 0 | // Safe because the chunks are always 8 bytes exactly. |
544 | 0 | chunk.copy_from_slice(&n); |
545 | 0 | } |
546 | | |
547 | 0 | let remainder = chunks.into_remainder(); |
548 | 0 |
|
549 | 0 | // Any remainder will always be less than 8 bytes. |
550 | 0 | if !remainder.is_empty() { |
551 | 0 | // Generate one last block of 8 bytes of entropy |
552 | 0 | let n = self.gen_u64().to_ne_bytes(); |
553 | 0 |
|
554 | 0 | // Use the remaining length to copy from block |
555 | 0 | remainder.copy_from_slice(&n[..remainder.len()]); |
556 | 0 | } |
557 | 0 | } |
558 | | |
559 | | rng_integer!( |
560 | | u8, |
561 | | u8, |
562 | | gen_u32, |
563 | | gen_mod_u32, |
564 | | "Generates a random `u8` in the given range." |
565 | | ); |
566 | | |
567 | | rng_integer!( |
568 | | u16, |
569 | | u16, |
570 | | gen_u32, |
571 | | gen_mod_u32, |
572 | | "Generates a random `u16` in the given range." |
573 | | ); |
574 | | |
575 | | rng_integer!( |
576 | | u32, |
577 | | u32, |
578 | | gen_u32, |
579 | | gen_mod_u32, |
580 | | "Generates a random `u32` in the given range." |
581 | | ); |
582 | | |
583 | | rng_integer!( |
584 | | u64, |
585 | | u64, |
586 | | gen_u64, |
587 | | gen_mod_u64, |
588 | | "Generates a random `u64` in the given range." |
589 | | ); |
590 | | |
591 | | rng_integer!( |
592 | | u128, |
593 | | u128, |
594 | | gen_u128, |
595 | | gen_mod_u128, |
596 | | "Generates a random `u128` in the given range." |
597 | | ); |
598 | | |
599 | | #[cfg(target_pointer_width = "16")] |
600 | | rng_integer!( |
601 | | usize, |
602 | | usize, |
603 | | gen_u32, |
604 | | gen_mod_u32, |
605 | | "Generates a random `usize` in the given range." |
606 | | ); |
607 | | #[cfg(target_pointer_width = "32")] |
608 | | rng_integer!( |
609 | | usize, |
610 | | usize, |
611 | | gen_u32, |
612 | | gen_mod_u32, |
613 | | "Generates a random `usize` in the given range." |
614 | | ); |
615 | | #[cfg(target_pointer_width = "64")] |
616 | | rng_integer!( |
617 | | usize, |
618 | | usize, |
619 | | gen_u64, |
620 | | gen_mod_u64, |
621 | | "Generates a random `usize` in the given range." |
622 | | ); |
623 | | |
624 | | /// Generates a random `char` in range A-Z. |
625 | | #[inline] |
626 | 0 | pub fn uppercase(&mut self) -> char { |
627 | | const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
628 | 0 | *self.choice(CHARS).unwrap() as char |
629 | 0 | } |
630 | | |
631 | | /// Generates a random `char` in the given range. |
632 | | /// |
633 | | /// Panics if the range is empty. |
634 | | #[inline] |
635 | 0 | pub fn char(&mut self, range: impl RangeBounds<char>) -> char { |
636 | 0 | let panic_empty_range = || { |
637 | 0 | panic!( |
638 | 0 | "empty range: {:?}..{:?}", |
639 | 0 | range.start_bound(), |
640 | 0 | range.end_bound() |
641 | 0 | ) |
642 | | }; |
643 | | |
644 | 0 | let surrogate_start = 0xd800u32; |
645 | 0 | let surrogate_len = 0x800u32; |
646 | | |
647 | 0 | let low = match range.start_bound() { |
648 | 0 | Bound::Unbounded => 0u8 as char, |
649 | 0 | Bound::Included(&x) => x, |
650 | 0 | Bound::Excluded(&x) => { |
651 | 0 | let scalar = if x as u32 == surrogate_start - 1 { |
652 | 0 | surrogate_start + surrogate_len |
653 | | } else { |
654 | 0 | x as u32 + 1 |
655 | | }; |
656 | 0 | char::try_from(scalar).unwrap_or_else(|_| panic_empty_range()) |
657 | | } |
658 | | }; |
659 | | |
660 | 0 | let high = match range.end_bound() { |
661 | 0 | Bound::Unbounded => core::char::MAX, |
662 | 0 | Bound::Included(&x) => x, |
663 | 0 | Bound::Excluded(&x) => { |
664 | 0 | let scalar = if x as u32 == surrogate_start + surrogate_len { |
665 | 0 | surrogate_start - 1 |
666 | | } else { |
667 | 0 | (x as u32).wrapping_sub(1) |
668 | | }; |
669 | 0 | char::try_from(scalar).unwrap_or_else(|_| panic_empty_range()) |
670 | | } |
671 | | }; |
672 | | |
673 | 0 | if low > high { |
674 | 0 | panic_empty_range(); |
675 | 0 | } |
676 | | |
677 | 0 | let gap = if (low as u32) < surrogate_start && (high as u32) >= surrogate_start { |
678 | 0 | surrogate_len |
679 | | } else { |
680 | 0 | 0 |
681 | | }; |
682 | 0 | let range = high as u32 - low as u32 - gap; |
683 | 0 | let mut val = self.u32(0..=range) + low as u32; |
684 | 0 | if val >= surrogate_start { |
685 | 0 | val += gap; |
686 | 0 | } |
687 | 0 | val.try_into().unwrap() |
688 | 0 | } |
689 | | } |