rand.rs 605 B

1234567891011121314151617181920212223242526
  1. #![allow(unsafe_code)]
  2. #![allow(unused)]
  3. #[derive(Debug)]
  4. pub(crate) struct Rand {
  5. state: u64,
  6. }
  7. impl Rand {
  8. pub(crate) const fn new(seed: u64) -> Self {
  9. Self { state: seed }
  10. }
  11. pub(crate) fn rand_u32(&mut self) -> u32 {
  12. // sPCG32 from https://www.pcg-random.org/paper.html
  13. // see also https://nullprogram.com/blog/2017/09/21/
  14. const M: u64 = 0xbb2efcec3c39611d;
  15. const A: u64 = 0x7590ef39;
  16. let s = self.state.wrapping_mul(M).wrapping_add(A);
  17. self.state = s;
  18. let shift = 29 - (s >> 61);
  19. (s >> shift) as u32
  20. }
  21. }