summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorYuriy Glukhov <yglukhov@users.noreply.github.com>2018-01-18 20:47:58 +0300
committerAndreas Rumpf <rumpf_a@web.de>2018-01-18 18:47:58 +0100
commit1b3f640188b06c6b6c1ead5ab4d28345a93ace73 (patch)
treef0333efc489ab17963aa9d1e598f88513acea3f5
parent33b0e739625d9894b2fbf1e019b69bae39e3db17 (diff)
downloadNim-1b3f640188b06c6b6c1ead5ab4d28345a93ace73.tar.gz
Fixed crash in rand (#7103)
-rw-r--r--lib/pure/random.nim5
1 files changed, 5 insertions, 0 deletions
diff --git a/lib/pure/random.nim b/lib/pure/random.nim
index de419b9fb..97ad12b99 100644
--- a/lib/pure/random.nim
+++ b/lib/pure/random.nim
@@ -115,6 +115,7 @@ proc rand*(r: var Rand; max: int): int {.benign.} =
   ## random number is always the same, unless `randomize` is called
   ## which initializes the random number generator with a "random"
   ## number, i.e. a tickcount.
+  if max == 0: return
   while true:
     let x = next(r)
     if x <= randMax - (randMax mod ui(max)):
@@ -213,4 +214,8 @@ when isMainModule:
     shuffle(a)
     doAssert a[0] == 1
     doAssert a[1] == 0
+
+    doAssert rand(0) == 0
+    doAssert rand("a") == 'a'
+
   main()