summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-01-11 06:13:27 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-01-11 06:13:27 +0100
commit767524d62a1ecf8bdab1457d08a51f77cedefca3 (patch)
tree5d359b61813528bf3bd881f2c8104ed33f701d0e /lib
parent17c62bb54546b4d2f574edeb955162600b4263f3 (diff)
downloadNim-767524d62a1ecf8bdab1457d08a51f77cedefca3.tar.gz
random.shuffle now takes an openArray proc
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/random.nim9
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/pure/random.nim b/lib/pure/random.nim
index 7bb54cb8e..b4681610a 100644
--- a/lib/pure/random.nim
+++ b/lib/pure/random.nim
@@ -106,9 +106,9 @@ proc randomize*(seed: int) {.benign.} =
   state.a0 = ui(seed shr 16)
   state.a1 = ui(seed and 0xffff)
 
-proc shuffle*[T](x: var seq[T]) =
+proc shuffle*[T](x: var openArray[T]) =
   ## Will randomly swap the positions of elements in a sequence.
-  for i in countdown(x.high, 0):
+  for i in countdown(x.high, 1):
     let j = random(i + 1)
     swap(x[i], x[j])
 
@@ -139,4 +139,9 @@ when isMainModule:
         doAssert false, "too few occurrences of " & $i
       elif oc > 130:
         doAssert false, "too many occurrences of " & $i
+
+    var a = [0, 1]
+    shuffle(a)
+    doAssert a[0] == 1
+    doAssert a[1] == 0
   main()