1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Fast non-cryptographic pseudo-random number generators.
//!
//! Provides xorshift-based PRNGs for `u32` and `u64` in two flavors:
//!
//! - **Stateful** ([`cheap_random_with_current_u32`], [`cheap_random_with_current_u64`]):
//! the caller owns the state. Use these when you need a reproducible
//! sequence or when thread-local storage is unavailable (e.g. `no_std`).
//!
//! - **Thread-local** ([`cheap_random_u32`], [`cheap_random_u64`]):
//! state is managed automatically per-thread. Available only when the
//! `no_std` feature is disabled.
//!
//! Neither generator is suitable for cryptographic use. Both produce
//! statistically adequate randomness for tasks such as treap priorities,
//! random sampling, and load balancing.
use ;
/// Advances `current` with a 32-bit xorshift step and returns the new value.
///
/// Suitable for use as a treap priority generator or any place a fast,
/// non-cryptographic `u32` sequence is needed.
///
/// # Example
///
/// ```rust
/// use orengine_utils::cheap_random::cheap_random_with_current_u32;
///
/// let mut state = core::num::NonZeroU32::new(1).unwrap();
///
/// let a = cheap_random_with_current_u32(&mut state);
/// let b = cheap_random_with_current_u32(&mut state);
///
/// assert_ne!(a, b);
/// ```
/// Returns a random `u32` from a thread-local xorshift state.
///
/// The state is initialized once per thread and advances automatically.
/// Equivalent to calling [`cheap_random_with_current_u32`] on a hidden
/// per-thread [`NonZeroU32`].
///
/// # Example
///
/// ```rust
/// use orengine_utils::cheap_random::cheap_random_u32;
///
/// let a = cheap_random_u32();
/// let b = cheap_random_u32();
///
/// assert_ne!(a, b);
/// ```
/// Advances `current` with a 64-bit xorshift step and returns the new value.
///
/// Suitable for any place a fast, non-cryptographic `u64` sequence is needed.
///
/// # Example
///
/// ```rust
/// use orengine_utils::cheap_random::cheap_random_with_current_u64;
///
/// let mut state = core::num::NonZeroU64::new(1).unwrap();
///
/// let a = cheap_random_with_current_u64(&mut state);
/// let b = cheap_random_with_current_u64(&mut state);
///
/// assert_ne!(a, b);
/// ```
/// Returns a random `u64` from a thread-local xorshift state.
///
/// The state is initialized once per thread and advances automatically.
/// Equivalent to calling [`cheap_random_with_current_u64`] on a hidden
/// per-thread [`NonZeroU64`].
///
/// # Example
///
/// ```rust
/// use orengine_utils::cheap_random::cheap_random_u64;
///
/// let a = cheap_random_u64();
/// let b = cheap_random_u64();
///
/// assert_ne!(a, b);
/// ```