random.nim 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Nim's standard random number generator. Based on
  10. ## the ``xoroshiro128+`` (xor/rotate/shift/rotate) library.
  11. ## * More information: http://xoroshiro.di.unimi.it/
  12. ## * C implementation: http://xoroshiro.di.unimi.it/xoroshiro128plus.c
  13. ##
  14. ## **Do not use this module for cryptographic purposes!**
  15. include "system/inclrtl"
  16. {.push debugger:off.}
  17. when defined(JS):
  18. type ui = uint32
  19. const randMax = 4_294_967_295u32
  20. else:
  21. type ui = uint64
  22. const randMax = 18_446_744_073_709_551_615u64
  23. type
  24. Rand* = object ## State of the random number generator.
  25. ## The procs that use the default state
  26. ## are **not** thread-safe!
  27. a0, a1: ui
  28. when defined(JS):
  29. var state = Rand(
  30. a0: 0x69B4C98Cu32,
  31. a1: 0xFED1DD30u32) # global for backwards compatibility
  32. else:
  33. # racy for multi-threading but good enough for now:
  34. var state = Rand(
  35. a0: 0x69B4C98CB8530805u64,
  36. a1: 0xFED1DD3004688D67CAu64) # global for backwards compatibility
  37. proc rotl(x, k: ui): ui =
  38. result = (x shl k) or (x shr (ui(64) - k))
  39. proc next*(r: var Rand): uint64 =
  40. ## Uses the state to compute a new ``uint64`` random number.
  41. let s0 = r.a0
  42. var s1 = r.a1
  43. result = s0 + s1
  44. s1 = s1 xor s0
  45. r.a0 = rotl(s0, 55) xor s1 xor (s1 shl 14) # a, b
  46. r.a1 = rotl(s1, 36) # c
  47. proc skipRandomNumbers*(s: var Rand) =
  48. ## This is the jump function for the generator. It is equivalent
  49. ## to 2^64 calls to next(); it can be used to generate 2^64
  50. ## non-overlapping subsequences for parallel computations.
  51. when defined(JS):
  52. const helper = [0xbeac0467u32, 0xd86b048bu32]
  53. else:
  54. const helper = [0xbeac0467eba5facbu64, 0xd86b048b86aa9922u64]
  55. var
  56. s0 = ui 0
  57. s1 = ui 0
  58. for i in 0..high(helper):
  59. for b in 0 ..< 64:
  60. if (helper[i] and (ui(1) shl ui(b))) != 0:
  61. s0 = s0 xor s.a0
  62. s1 = s1 xor s.a1
  63. discard next(s)
  64. s.a0 = s0
  65. s.a1 = s1
  66. proc random*(max: int): int {.benign, deprecated.} =
  67. ## Returns a random number in the range 0..max-1. The sequence of
  68. ## random number is always the same, unless `randomize` is called
  69. ## which initializes the random number generator with a "random"
  70. ## number, i.e. a tickcount. **Deprecated since version 0.18.0**.
  71. ## Use ``rand`` instead.
  72. while true:
  73. let x = next(state)
  74. if x < randMax - (randMax mod ui(max)):
  75. return int(x mod uint64(max))
  76. proc random*(max: float): float {.benign, deprecated.} =
  77. ## Returns a random number in the range 0..<max. The sequence of
  78. ## random number is always the same, unless `randomize` is called
  79. ## which initializes the random number generator with a "random"
  80. ## number, i.e. a tickcount. **Deprecated since version 0.18.0**.
  81. ## Use ``rand`` instead.
  82. let x = next(state)
  83. when defined(JS):
  84. result = (float(x) / float(high(uint32))) * max
  85. else:
  86. let u = (0x3FFu64 shl 52u64) or (x shr 12u64)
  87. result = (cast[float](u) - 1.0) * max
  88. proc random*[T](x: HSlice[T, T]): T {.deprecated.} =
  89. ## For a slice `a .. b` returns a value in the range `a .. b-1`.
  90. ## **Deprecated since version 0.18.0**.
  91. ## Use ``rand`` instead.
  92. result = T(random(x.b - x.a)) + x.a
  93. proc random*[T](a: openArray[T]): T {.deprecated.} =
  94. ## returns a random element from the openarray `a`.
  95. ## **Deprecated since version 0.18.0**.
  96. ## Use ``rand`` instead.
  97. result = a[random(a.low..a.len)]
  98. proc rand*(r: var Rand; max: Natural): int {.benign.} =
  99. ## Returns a random number in the range 0..max. The sequence of
  100. ## random number is always the same, unless `randomize` is called
  101. ## which initializes the random number generator with a "random"
  102. ## number, i.e. a tickcount.
  103. if max == 0: return
  104. while true:
  105. let x = next(r)
  106. if x <= randMax - (randMax mod ui(max)):
  107. return int(x mod (uint64(max)+1u64))
  108. proc rand*(max: int): int {.benign.} =
  109. ## Returns a random number in the range 0..max. The sequence of
  110. ## random number is always the same, unless `randomize` is called
  111. ## which initializes the random number generator with a "random"
  112. ## number, i.e. a tickcount.
  113. rand(state, max)
  114. proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.benign.} =
  115. ## Returns a random number in the range 0..max. The sequence of
  116. ## random number is always the same, unless `randomize` is called
  117. ## which initializes the random number generator with a "random"
  118. ## number, i.e. a tickcount.
  119. let x = next(r)
  120. when defined(JS):
  121. result = (float(x) / float(high(uint32))) * max
  122. else:
  123. let u = (0x3FFu64 shl 52u64) or (x shr 12u64)
  124. result = (cast[float](u) - 1.0) * max
  125. proc rand*(max: float): float {.benign.} =
  126. ## Returns a random number in the range 0..max. The sequence of
  127. ## random number is always the same, unless `randomize` is called
  128. ## which initializes the random number generator with a "random"
  129. ## number, i.e. a tickcount.
  130. rand(state, max)
  131. proc rand*[T](r: var Rand; x: HSlice[T, T]): T =
  132. ## For a slice `a .. b` returns a value in the range `a .. b`.
  133. result = T(rand(r, x.b - x.a)) + x.a
  134. proc rand*[T](x: HSlice[T, T]): T =
  135. ## For a slice `a .. b` returns a value in the range `a .. b`.
  136. result = rand(state, x)
  137. proc rand*[T](r: var Rand; a: openArray[T]): T =
  138. ## returns a random element from the openarray `a`.
  139. result = a[rand(r, a.low..a.high)]
  140. proc rand*[T](a: openArray[T]): T =
  141. ## returns a random element from the openarray `a`.
  142. result = a[rand(a.low..a.high)]
  143. proc initRand*(seed: int64): Rand =
  144. ## Creates a new ``Rand`` state from ``seed``.
  145. result.a0 = ui(seed shr 16)
  146. result.a1 = ui(seed and 0xffff)
  147. discard next(result)
  148. proc randomize*(seed: int64) {.benign.} =
  149. ## Initializes the default random number generator
  150. ## with a specific seed.
  151. state = initRand(seed)
  152. proc shuffle*[T](r: var Rand; x: var openArray[T]) =
  153. ## Swaps the positions of elements in a sequence randomly.
  154. for i in countdown(x.high, 1):
  155. let j = r.rand(i)
  156. swap(x[i], x[j])
  157. proc shuffle*[T](x: var openArray[T]) =
  158. ## Swaps the positions of elements in a sequence randomly.
  159. shuffle(state, x)
  160. when not defined(nimscript):
  161. import times
  162. proc randomize*() {.benign.} =
  163. ## Initializes the random number generator with a "random"
  164. ## number, i.e. a tickcount. Note: Does not work for NimScript.
  165. let now = times.getTime()
  166. randomize(convert(Seconds, Nanoseconds, now.toUnix) + now.nanosecond)
  167. {.pop.}
  168. when isMainModule:
  169. proc main =
  170. var occur: array[1000, int]
  171. var x = 8234
  172. for i in 0..100_000:
  173. x = rand(high(occur))
  174. inc occur[x]
  175. for i, oc in occur:
  176. if oc < 69:
  177. doAssert false, "too few occurrences of " & $i
  178. elif oc > 150:
  179. doAssert false, "too many occurrences of " & $i
  180. var a = [0, 1]
  181. shuffle(a)
  182. doAssert a[0] == 1
  183. doAssert a[1] == 0
  184. doAssert rand(0) == 0
  185. doAssert rand("a") == 'a'
  186. when compileOption("rangeChecks"):
  187. try:
  188. discard rand(-1)
  189. doAssert false
  190. except RangeError:
  191. discard
  192. try:
  193. discard rand(-1.0)
  194. doAssert false
  195. except RangeError:
  196. discard
  197. main()