rand.odin 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package rand
  2. import "core:intrinsics"
  3. Rand :: struct {
  4. state: u64,
  5. inc: u64,
  6. }
  7. @(private)
  8. global_rand := create(u64(intrinsics.read_cycle_counter()))
  9. set_global_seed :: proc(seed: u64) {
  10. init(&global_rand, seed)
  11. }
  12. create :: proc(seed: u64) -> Rand {
  13. r: Rand
  14. init(&r, seed)
  15. return r
  16. }
  17. init :: proc(r: ^Rand, seed: u64) {
  18. r.state = 0
  19. r.inc = (seed << 1) | 1
  20. _random(r)
  21. r.state += seed
  22. _random(r)
  23. }
  24. _random :: proc(r: ^Rand) -> u32 {
  25. r := r
  26. if r == nil {
  27. // NOTE(bill, 2020-09-07): Do this so that people can
  28. // enforce the global random state if necessary with `nil`
  29. r = &global_rand
  30. }
  31. old_state := r.state
  32. r.state = old_state * 6364136223846793005 + (r.inc|1)
  33. xor_shifted := u32(((old_state>>18) ~ old_state) >> 27)
  34. rot := u32(old_state >> 59)
  35. return (xor_shifted >> rot) | (xor_shifted << ((-rot) & 31))
  36. }
  37. uint32 :: proc(r: ^Rand = nil) -> u32 { return _random(r) }
  38. uint64 :: proc(r: ^Rand = nil) -> u64 {
  39. a := u64(_random(r))
  40. b := u64(_random(r))
  41. return (a<<32) | b
  42. }
  43. uint128 :: proc(r: ^Rand = nil) -> u128 {
  44. a := u128(_random(r))
  45. b := u128(_random(r))
  46. c := u128(_random(r))
  47. d := u128(_random(r))
  48. return (a<<96) | (b<<64) | (c<<32) | d
  49. }
  50. int31 :: proc(r: ^Rand = nil) -> i32 { return i32(uint32(r) << 1 >> 1) }
  51. int63 :: proc(r: ^Rand = nil) -> i64 { return i64(uint64(r) << 1 >> 1) }
  52. int127 :: proc(r: ^Rand = nil) -> i128 { return i128(uint128(r) << 1 >> 1) }
  53. int31_max :: proc(n: i32, r: ^Rand = nil) -> i32 {
  54. if n <= 0 {
  55. panic("Invalid argument to int31_max")
  56. }
  57. if n&(n-1) == 0 {
  58. return int31(r) & (n-1)
  59. }
  60. max := i32((1<<31) - 1 - (1<<31)%u32(n))
  61. v := int31(r)
  62. for v > max {
  63. v = int31(r)
  64. }
  65. return v % n
  66. }
  67. int63_max :: proc(n: i64, r: ^Rand = nil) -> i64 {
  68. if n <= 0 {
  69. panic("Invalid argument to int63_max")
  70. }
  71. if n&(n-1) == 0 {
  72. return int63(r) & (n-1)
  73. }
  74. max := i64((1<<63) - 1 - (1<<63)%u64(n))
  75. v := int63(r)
  76. for v > max {
  77. v = int63(r)
  78. }
  79. return v % n
  80. }
  81. int127_max :: proc(n: i128, r: ^Rand = nil) -> i128 {
  82. if n <= 0 {
  83. panic("Invalid argument to int127_max")
  84. }
  85. if n&(n-1) == 0 {
  86. return int127(r) & (n-1)
  87. }
  88. max := i128((1<<127) - 1 - (1<<127)%u128(n))
  89. v := int127(r)
  90. for v > max {
  91. v = int127(r)
  92. }
  93. return v % n
  94. }
  95. int_max :: proc(n: int, r: ^Rand = nil) -> int {
  96. if n <= 0 {
  97. panic("Invalid argument to int_max")
  98. }
  99. when size_of(int) == 4 {
  100. return int(int31_max(i32(n), r))
  101. } else {
  102. return int(int63_max(i64(n), r))
  103. }
  104. }
  105. float64 :: proc(r: ^Rand = nil) -> f64 { return f64(int63_max(1<<53, r)) / (1 << 53) }
  106. float32 :: proc(r: ^Rand = nil) -> f32 { return f32(float64(r)) }
  107. float64_range :: proc(lo, hi: f64, r: ^Rand = nil) -> f64 { return (hi-lo)*float64(r) + lo }
  108. float32_range :: proc(lo, hi: f32, r: ^Rand = nil) -> f32 { return (hi-lo)*float32(r) + lo }
  109. read :: proc(p: []byte, r: ^Rand = nil) -> (n: int) {
  110. pos := i8(0)
  111. val := i64(0)
  112. for n = 0; n < len(p); n += 1 {
  113. if pos == 0 {
  114. val = int63(r)
  115. pos = 7
  116. }
  117. p[n] = byte(val)
  118. val >>= 8
  119. pos -= 1
  120. }
  121. return
  122. }
  123. // perm returns a slice of n ints in a pseudo-random permutation of integers in the range [0, n)
  124. perm :: proc(n: int, r: ^Rand = nil, allocator := context.allocator) -> []int {
  125. m := make([]int, n, allocator)
  126. for i := 0; i < n; i += 1 {
  127. j := int_max(i+1, r)
  128. m[i] = m[j]
  129. m[j] = i
  130. }
  131. return m
  132. }
  133. shuffle :: proc(array: $T/[]$E, r: ^Rand = nil) {
  134. n := i64(len(array))
  135. if n < 2 {
  136. return
  137. }
  138. for i := i64(0); i < n; i += 1 {
  139. j := int63_max(n, r)
  140. array[i], array[j] = array[j], array[i]
  141. }
  142. }