]> git.nega.tv - josh/narcissus/commitdiff
narcissus-core: Add `array_select` to Pcg64
authorJoshua Simmons <josh@nega.tv>
Sat, 29 Jul 2023 08:27:39 +0000 (10:27 +0200)
committerJoshua Simmons <josh@nega.tv>
Sat, 29 Jul 2023 08:27:39 +0000 (10:27 +0200)
libs/narcissus-core/src/rand.rs

index dcf4250a22bc614230ac02ed2bc6902ddd33b5c8..e46c2065543ae93322bdfe1eb5b3b0bad955126e 100644 (file)
@@ -83,6 +83,39 @@ impl Pcg64 {
         }
     }
 
+    /// Randomly select an an element from `slice` with uniform probability.
+    ///
+    /// Always draws two 64 bit words from the PRNG.
+    pub fn select_mut<'a, T>(&mut self, slice: &'a mut [T]) -> Option<&'a mut T> {
+        if slice.is_empty() {
+            None
+        } else {
+            slice.get_mut(self.next_bound_usize(slice.len()))
+        }
+    }
+
+    /// Randomly select an an element from `array` with uniform probability.
+    ///
+    /// Always draws two 64 bit words from the PRNG.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `N` is 0
+    pub fn array_select<'a, T, const N: usize>(&mut self, array: &'a [T; N]) -> &'a T {
+        &array[self.next_bound_usize(N)]
+    }
+
+    /// Randomly select an an element from `array` with uniform probability.
+    ///
+    /// Always draws two 64 bit words from the PRNG.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `N` is 0
+    pub fn array_select_mut<'a, T, const N: usize>(&mut self, array: &'a mut [T; N]) -> &'a mut T {
+        &mut array[self.next_bound_usize(N)]
+    }
+
     /// Shuffle the elements in `slice` in-place.
     ///
     /// Note that as `Pcg64` is initialized with a 128 bit seed, it's only possible