/rust/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.53.3/src/util/rand.rs
Line | Count | Source |
1 | | cfg_rt! { |
2 | | mod rt; |
3 | | pub(crate) use rt::RngSeedGenerator; |
4 | | |
5 | | cfg_unstable! { |
6 | | mod rt_unstable; |
7 | | } |
8 | | } |
9 | | |
10 | | /// A seed for random number generation. |
11 | | /// |
12 | | /// In order to make certain functions within a runtime deterministic, a seed |
13 | | /// can be specified at the time of creation. |
14 | | #[allow(unreachable_pub)] |
15 | | #[derive(Clone, Debug)] |
16 | | pub struct RngSeed { |
17 | | s: u32, |
18 | | r: u32, |
19 | | } |
20 | | |
21 | | /// Fast random number generate. |
22 | | /// |
23 | | /// Implement `xorshift64+`: 2 32-bit `xorshift` sequences added together. |
24 | | /// Shift triplet `[17,7,16]` was calculated as indicated in Marsaglia's |
25 | | /// `Xorshift` paper: <https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf> |
26 | | /// This generator passes the SmallCrush suite, part of TestU01 framework: |
27 | | /// <http://simul.iro.umontreal.ca/testu01/tu01.html> |
28 | | #[derive(Clone, Copy, Debug)] |
29 | | pub(crate) struct FastRand { |
30 | | one: u32, |
31 | | two: u32, |
32 | | } |
33 | | |
34 | | impl RngSeed { |
35 | | /// Creates a random seed using loom internally. |
36 | 582k | pub(crate) fn new() -> Self { |
37 | 582k | Self::from_u64(crate::loom::rand::seed()) |
38 | 582k | } |
39 | | |
40 | 582k | fn from_u64(seed: u64) -> Self { |
41 | 582k | let one = (seed >> 32) as u32; |
42 | 582k | let two = seed as u32; |
43 | | |
44 | 582k | Self::from_pair(one, two) |
45 | 582k | } |
46 | | |
47 | 2.93M | fn from_pair(s: u32, r: u32) -> Self { |
48 | 2.93M | if s == 0 && r == 0 { |
49 | 0 | Self { s: 0, r: 1 } |
50 | | } else { |
51 | 2.93M | Self { s, r } |
52 | | } |
53 | 2.93M | } |
54 | | } |
55 | | |
56 | | impl FastRand { |
57 | | /// Initialize a new fast random number generator using the default source of entropy. |
58 | 564k | pub(crate) fn new() -> FastRand { |
59 | 564k | FastRand::from_seed(RngSeed::new()) |
60 | 564k | } |
61 | | |
62 | | /// Initializes a new, thread-local, fast random number generator. |
63 | 1.18M | pub(crate) fn from_seed(seed: RngSeed) -> FastRand { |
64 | 1.18M | FastRand { |
65 | 1.18M | one: seed.s, |
66 | 1.18M | two: seed.r, |
67 | 1.18M | } |
68 | 1.18M | } |
69 | | |
70 | | #[cfg(any( |
71 | | feature = "macros", |
72 | | feature = "rt-multi-thread", |
73 | | all(feature = "sync", feature = "rt") |
74 | | ))] |
75 | 1.13M | pub(crate) fn fastrand_n(&mut self, n: u32) -> u32 { |
76 | | // This is similar to fastrand() % n, but faster. |
77 | | // See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ |
78 | 1.13M | let mul = (self.fastrand() as u64).wrapping_mul(n as u64); |
79 | 1.13M | (mul >> 32) as u32 |
80 | 1.13M | } |
81 | | |
82 | 3.50M | fn fastrand(&mut self) -> u32 { |
83 | 3.50M | let mut s1 = self.one; |
84 | 3.50M | let s0 = self.two; |
85 | | |
86 | 3.50M | s1 ^= s1 << 17; |
87 | 3.50M | s1 = s1 ^ s0 ^ s1 >> 7 ^ s0 >> 16; |
88 | | |
89 | 3.50M | self.one = s0; |
90 | 3.50M | self.two = s1; |
91 | | |
92 | 3.50M | s0.wrapping_add(s1) |
93 | 3.50M | } |
94 | | } |
95 | | |
96 | | #[cfg(test)] |
97 | | mod tests { |
98 | | use super::*; |
99 | | |
100 | | #[test] |
101 | | fn non_zero_seed_from_u64() { |
102 | | let seed = RngSeed::from_u64(0); |
103 | | assert_eq!(seed.s, 0); |
104 | | assert_eq!(seed.r, 1); |
105 | | } |
106 | | |
107 | | #[test] |
108 | | fn non_zero_seed_from_pair() { |
109 | | let seed = RngSeed::from_pair(0, 0); |
110 | | assert_eq!(seed.s, 0); |
111 | | assert_eq!(seed.r, 1); |
112 | | } |
113 | | } |