summary refs log tree commit diff stats
path: root/lib/pure/random.nim
diff options
context:
space:
mode:
authorQuelklef <elimaynard923@gmail.com>2018-07-30 04:19:11 -0400
committerAndreas Rumpf <rumpf_a@web.de>2018-07-30 10:19:11 +0200
commita4c244aef08b25542323fed1da9d7dfa8f230cf1 (patch)
tree8265aeb4ea73fee1d6279d57b9ccb73698ee5290 /lib/pure/random.nim
parent2569c749097135d5c9a8e2153bed6f0bad3a6684 (diff)
downloadNim-a4c244aef08b25542323fed1da9d7dfa8f230cf1.tar.gz
Add checks for random.rand() (#8431)
Diffstat (limited to 'lib/pure/random.nim')
-rw-r--r--lib/pure/random.nim17
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/pure/random.nim b/lib/pure/random.nim
index 01ea9c845..e565fccf8 100644
--- a/lib/pure/random.nim
+++ b/lib/pure/random.nim
@@ -110,7 +110,7 @@ proc random*[T](a: openArray[T]): T {.deprecated.} =
   ## Use ``rand`` instead.
   result = a[random(a.low..a.len)]
 
-proc rand*(r: var Rand; max: int): int {.benign.} =
+proc rand*(r: var Rand; max: Natural): int {.benign.} =
   ## Returns a random number in the range 0..max. The sequence of
   ## random number is always the same, unless `randomize` is called
   ## which initializes the random number generator with a "random"
@@ -128,7 +128,7 @@ proc rand*(max: int): int {.benign.} =
   ## number, i.e. a tickcount.
   rand(state, max)
 
-proc rand*(r: var Rand; max: float): float {.benign.} =
+proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.benign.} =
   ## Returns a random number in the range 0..max. The sequence of
   ## random number is always the same, unless `randomize` is called
   ## which initializes the random number generator with a "random"
@@ -218,4 +218,17 @@ when isMainModule:
     doAssert rand(0) == 0
     doAssert rand("a") == 'a'
 
+    when compileOption("rangeChecks"):
+      try:
+        discard rand(-1)
+        doAssert false
+      except RangeError:
+        discard
+
+      try:
+        discard rand(-1.0)
+        doAssert false
+      except RangeError:
+        discard
+
   main()