Skip to content

Commit

Permalink
document behavior for seed == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Mar 23, 2021
1 parent 8aa228d commit 530d454
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/pure/random.nim
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ proc initRand*(seed: int64): Rand =
##
## Providing a specific seed will produce the same results for that seed each time.
##
## The resulting state is independent of the default RNG's state.
## The resulting state is independent of the default RNG's state. When `seed == 0`,
## we internally set the seed to an implementation defined non-zero value.
##
## **See also:**
## * `initRand proc<#initRand>`_ that uses the current time
Expand All @@ -559,10 +560,10 @@ proc initRand*(seed: int64): Rand =
from std/times import getTime, toUnix, nanosecond

var r1 = initRand(123)

let now = getTime()
var r2 = initRand(now.toUnix * 1_000_000_000 + now.nanosecond)
let seed = if seed != 0: seed else: 1 # because 0 is a fixed point
const seedFallback0 = int64.high # arbitrary
let seed = if seed != 0: seed else: seedFallback0 # because 0 is a fixed point
result.a0 = Ui(seed shr 16)
result.a1 = Ui(seed and 0xffff)
when not defined(nimLegacyRandomInitRand):
Expand Down

0 comments on commit 530d454

Please sign in to comment.