Fix initrand to avoid random number sequences overlapping (#18744)

* Fix initrand to avoid random number sequences overlapping

* Minor fix

* Fix compile error on js backend

* Disable new test for js backend

* Minor fix

* tempfiles module uses random.initRand()

* Remove unused module import from lib/std/tempfiles.nim

* Initialize baseState in initRand()

* Run tests/stdlib/trandom.nim from tests/test_nimscript.nims

* baseState is initialized only with sysrand.urandom and quit if failed

* Add comments
This commit is contained in:
Tomohiro
2021-09-02 21:12:14 +09:00
committed by GitHub
parent e0ef859130
commit 7c8ea490a2
4 changed files with 95 additions and 20 deletions

View File

@@ -17,7 +17,7 @@ See also:
* `mkstemp` (posix), refs https://man7.org/linux/man-pages/man3/mkstemp.3.html
]#
import os, random, std/monotimes
import os, random
const
@@ -107,11 +107,8 @@ var nimTempPathState {.threadvar.}: NimTempPathState
template randomPathName(length: Natural): string =
var res = newString(length)
if not nimTempPathState.isInit:
var time = getMonoTime().ticks
when compileOption("threads"):
time = time xor int64(getThreadId())
nimTempPathState.isInit = true
nimTempPathState.state = initRand(time)
nimTempPathState.state = initRand()
for i in 0 ..< length:
res[i] = nimTempPathState.state.sample(letters)