Skip to content

Commit

Permalink
Make random_choice more general.
Browse files Browse the repository at this point in the history
  • Loading branch information
wsx-antithesis committed Apr 23, 2024
1 parent f20e220 commit f36e994
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ pub fn get_random() -> u64 {
internal::dispatch_random()
}

pub fn random_choice<T: Copy>(slice: &[T]) -> Option<T> {
let lx: usize = slice.len();
match lx {
0 => None,
1 => Some(slice[0]),
pub fn random_choice<T>(slice: &[T]) -> Option<&T> {
match slice {
[] => None,
[x] => Some(x),
_ => {
let idx: usize = (get_random() as usize) % lx;
Some(slice[idx])
let idx: usize = (get_random() as usize) % slice.len();
Some(&slice[idx])
}
}
}
Expand All @@ -33,7 +32,7 @@ mod tests {
fn random_choice_one_choice() {
let array = ["ABc"; 1];
assert_eq!(1, array.len());
assert_eq!(Some("ABc"), random_choice(&array))
assert_eq!(Some(&"ABc"), random_choice(&array))
}


Expand All @@ -52,7 +51,7 @@ mod tests {
for _i in 0..15 {
let rc = random_choice(all_keys.as_slice());
if let Some(choice) = rc {
if let Some(x) = counted_items.get_mut(&choice) {
if let Some(x) = counted_items.get_mut(choice) {
*x += 1;
}
}
Expand Down

0 comments on commit f36e994

Please sign in to comment.