summary refs log tree commit diff stats
path: root/lib/pure/random.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/random.nim')
-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 de419b9fb..01ea9c845 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)):
@@ -190,8 +191,8 @@ when not defined(nimscript):
   proc randomize*() {.benign.} =
     ## Initializes the random number generator with a "random"
     ## number, i.e. a tickcount. Note: Does not work for NimScript.
-    let time = int64(times.epochTime() * 1_000_000_000)
-    randomize(time)
+    let now = times.getTime()
+    randomize(convert(Seconds, Nanoseconds, now.toUnix) + now.nanosecond)
 
 {.pop.}
 
@@ -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()