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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::{WallSwitchError, WallSwitchResult};
use std::hash::{BuildHasher, Hasher, RandomState};
/// Trait to extend slices with shuffling and random selection capabilities.
pub trait RandomExt {
/// The type of elements in the slice.
type Item;
/// Shuffles the elements of the slice in place using the Fisher-Yates algorithm.
///
/// This operation runs in $O(N)$ time complexity and modifies the collection
/// directly without allocating extra memory.
///
/// # Examples
///
/// ```
/// use wallswitch::RandomExt;
///
/// let mut strings = vec!["abc", "foo", "bar", "baz", "mm nn", "zzz"];
/// strings.shuffle();
///
/// let mut integers: Vec<u32> = (1..=20).collect();
/// integers.shuffle();
/// ```
///
/// See: <https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle>
fn shuffle(&mut self);
/// Chooses a random reference to an element from the slice.
///
/// Returns `None` if the slice is empty.
///
/// # Examples
///
/// ```
/// use wallswitch::RandomExt;
///
/// let items = vec![10, 20, 30];
/// let chosen = items.choose();
/// assert!(chosen.is_some());
/// assert!(items.contains(chosen.unwrap()));
/// ```
fn choose(&self) -> Option<&Self::Item>;
/// Selects a random copy of an element from the slice.
///
/// This is a convenience method that combines choosing an element and copying it.
/// It returns a [`WallSwitchResult`] to support clean error propagation using
/// the `?` operator.
///
/// # Errors
///
/// Returns [`WallSwitchError::EmptySlice`](crate::WallSwitchError::EmptySlice)
/// if the slice is empty.
///
/// # Examples
///
/// ```
/// use wallswitch::{RandomExt, WallSwitchResult};
///
/// # fn main() -> WallSwitchResult<()> {
/// let items = vec![10, 20, 30];
/// let chosen = items.get_random_sample()?;
/// assert!(items.contains(&chosen));
/// # Ok(())
/// # }
/// ```
fn get_random_sample(&self) -> WallSwitchResult<Self::Item>
where
Self::Item: Copy;
/// Selects a random cloned copy of an element from the slice.
///
/// This is a convenience method that combines choosing an element and cloning it,
/// supporting types that do not implement the `Copy` trait.
///
/// # Errors
///
/// Returns [`WallSwitchError::EmptySlice`](crate::WallSwitchError::EmptySlice)
/// if the slice is empty.
fn get_random_sample_cloned(&self) -> WallSwitchResult<Self::Item>
where
Self::Item: Clone;
}
impl<T> RandomExt for [T] {
type Item = T;
fn shuffle(&mut self) {
let n: usize = self.len();
if n < 2 {
return;
}
for i in 0..(n - 1) {
// Generate random index j, such that: i <= j < n
let j = rand::<usize>() % (n - i) + i;
self.swap(i, j);
}
}
fn choose(&self) -> Option<&Self::Item> {
if self.is_empty() {
None
} else {
let idx = rand::<usize>() % self.len();
Some(&self[idx])
}
}
fn get_random_sample(&self) -> WallSwitchResult<Self::Item>
where
Self::Item: Copy,
{
self.choose().copied().ok_or(WallSwitchError::EmptySlice)
}
fn get_random_sample_cloned(&self) -> WallSwitchResult<Self::Item>
where
Self::Item: Clone,
{
self.choose().cloned().ok_or(WallSwitchError::EmptySlice)
}
}
// --- Rand (Unificado) --- //
/// Helper function to generate a raw 64-bit unsigned integer using the system's `RandomState`.
#[inline]
fn raw_u64() -> u64 {
RandomState::new().build_hasher().finish()
}
/// A trait for types that can be generated randomly.
pub trait Rand {
/// Generates a random instance of the implementing type.
fn rand() -> Self;
}
macro_rules! impl_rand_int {
($($t:ty),*) => {
$(
impl Rand for $t {
#[inline]
fn rand() -> Self {
raw_u64() as $t
}
}
)*
};
}
// Implements Rand for all standard integer types by casting the raw u64 value.
impl_rand_int!(
u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize
);
impl Rand for f64 {
#[inline]
fn rand() -> Self {
(raw_u64() as f64) / (u64::MAX as f64)
}
}
impl Rand for f32 {
#[inline]
fn rand() -> Self {
(raw_u64() as f32) / (u64::MAX as f32)
}
}
/// Generates a random value of type `T`.
///
/// For integer types, this generates a value across the entire range of the type.
/// For floating-point types (`f32` and `f64`), this generates a value in the range `[0.0, 1.0)`.
///
/// # Examples
///
/// ```
/// use wallswitch::random::rand;
///
/// let random_u32: u32 = rand();
/// let random_float: f64 = rand();
/// assert!(random_float >= 0.0 && random_float < 1.0);
/// ```
#[inline]
pub fn rand<T: Rand>() -> T {
T::rand()
}
// --- Random Cast --- //
/// Helper trait to handle type-safe, generic casting for random integer outputs.
pub trait RandomCast {
/// Converts a raw 64-bit unsigned integer into the target numeric type.
fn from_u64(val: u64) -> Self;
}
macro_rules! impl_random_cast {
($($t:ty),*) => {
$(
impl RandomCast for $t {
#[inline]
fn from_u64(val: u64) -> Self {
val as $t
}
}
)*
};
}
impl_random_cast!(
u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64
);
// --- Get Random Integer --- //
/// Generate a random integer value in the given range (min, max) inclusive.
///
/// Accepts signed, unsigned, and platform-specific integer types for boundaries,
/// and automatically casts the result to the inferred numeric return type.
///
/// # Examples
///
/// ```
/// use wallswitch::get_random_integer;
///
/// let angle: f64 = get_random_integer(0, 359);
/// let idx: usize = get_random_integer(0, 10);
/// let iter: u32 = get_random_integer(300, 990);
/// ```
pub fn get_random_integer<T, R>(min: T, max: T) -> R
where
T: TryInto<u64>,
R: RandomCast,
{
let min_val = min.try_into().ok().unwrap_or(0);
let max_val = max.try_into().ok().unwrap_or(0);
let raw_val = if min_val >= max_val {
min_val
} else {
min_val + rand::<u64>() % (max_val - min_val + 1)
};
R::from_u64(raw_val)
}
/// Generate a random integer value in the given range (min, max) inclusive.
///
/// Returns an error if `min > max` and automatically casts the output to the inferred type.
pub fn get_random_integer_safe<R>(min: u64, max: u64) -> WallSwitchResult<R>
where
R: RandomCast,
{
if min > max {
Err(WallSwitchError::MinMax { min, max })
} else {
let raw_val = min + rand::<u64>() % (max - min + 1);
Ok(R::from_u64(raw_val))
}
}
//----------------------------------------------------------------------------//
// Tests //
//----------------------------------------------------------------------------//
#[cfg(test)]
mod tests_random {
use super::*;
#[test]
fn test_shuffle_and_rand() {
let mut data: Vec<usize> = (0..100).collect();
let original = data.clone();
data.shuffle();
assert_eq!(data.len(), 100);
// It is statistically highly improbable for a shuffled 100-element vector to remain unchanged
assert_ne!(data, original);
}
#[test]
fn test_choose() {
let data: Vec<usize> = (0..10).collect();
let chosen = data.choose();
assert!(chosen.is_some());
assert!(data.contains(chosen.unwrap()));
let empty_data: Vec<usize> = vec![];
assert!(empty_data.choose().is_none());
}
#[test]
fn test_automatic_inference_casts() {
// Automatically infers and converts to diverse destination types
let val_usize: usize = get_random_integer(10, 20);
assert!((10..=20).contains(&val_usize));
let val_u32: u32 = get_random_integer(100, 200);
assert!((100..=200).contains(&val_u32));
let val_f64: f64 = get_random_integer(0, 359);
assert!((0.0..=359.0).contains(&val_f64));
let val_i32: i32 = get_random_integer(1, 5);
assert!((1..=5).contains(&val_i32));
}
#[test]
fn test_safe_random_bounds() {
let valid: Result<u32, _> = get_random_integer_safe(50, 100);
assert!(valid.is_ok());
let val = valid.unwrap();
assert!((50..=100).contains(&val));
let invalid: Result<u32, _> = get_random_integer_safe(100, 50);
assert!(invalid.is_err());
}
#[test]
fn test_get_random_sample_success() {
let data = [42, 100, 200];
let result: WallSwitchResult<i32> = data.get_random_sample();
assert!(result.is_ok());
let value = result.unwrap();
assert!(data.contains(&value));
}
#[test]
fn test_get_random_sample_empty() {
let empty_data: Vec<i32> = vec![];
let result: WallSwitchResult<i32> = empty_data.get_random_sample();
assert!(result.is_err());
match result {
Err(WallSwitchError::EmptySlice) => {} // Sucesso: o erro correto foi retornado
_ => panic!("Expected WallSwitchError::EmptySlice, but got a different result"),
}
}
}