Splitmix64.sf 713 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/ruby
  2. # https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64
  3. class Splitmix64(state) {
  4. define (
  5. mask64 = (2**64 - 1)
  6. )
  7. method next_int {
  8. var n = (state = ((state + 0x9e3779b97f4a7c15) & mask64))
  9. n = ((n ^ (n >> 30)) * 0xbf58476d1ce4e5b9 & mask64)
  10. n = ((n ^ (n >> 27)) * 0x94d049bb133111eb & mask64)
  11. (n ^ (n >> 31)) & mask64
  12. }
  13. method next_float {
  14. self.next_int / (mask64+1) -> float
  15. }
  16. }
  17. say 'Seed: 1234567, first 5 values:'
  18. var rng = Splitmix64(1234567)
  19. var arr = 5.of { rng.next_int }
  20. assert_eq(arr, [6457827717110365317, 3203168211198807973, 9817491932198370423, 4593380528125082431, 16408922859458223821])
  21. say arr