Add initRand() with seed based on time (#16953)
This commit is contained in:
parent
c548f97241
commit
4fac8af0c9
2 changed files with 37 additions and 10 deletions
|
|
@ -124,6 +124,8 @@ with other backends. see #9125. Use `-d:nimLegacyJsRound` for previous behavior.
|
||||||
(instead of skipping them sometimes as it was before).
|
(instead of skipping them sometimes as it was before).
|
||||||
- Added optional `followSymlinks` argument to `setFilePermissions`.
|
- Added optional `followSymlinks` argument to `setFilePermissions`.
|
||||||
|
|
||||||
|
- Added `random.initRand()` overload with no argument which uses the current time as a seed.
|
||||||
|
|
||||||
## Language changes
|
## Language changes
|
||||||
|
|
||||||
- `nimscript` now handles `except Exception as e`.
|
- `nimscript` now handles `except Exception as e`.
|
||||||
|
|
|
||||||
|
|
@ -566,6 +566,7 @@ proc initRand*(seed: int64): Rand =
|
||||||
## generator's state.
|
## generator's state.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
|
## * `initRand proc<#initRand>`_ that uses the current time
|
||||||
## * `randomize proc<#randomize,int64>`_ that accepts a seed for the default
|
## * `randomize proc<#randomize,int64>`_ that accepts a seed for the default
|
||||||
## random number generator
|
## random number generator
|
||||||
## * `randomize proc<#randomize>`_ that initializes the default random
|
## * `randomize proc<#randomize>`_ that initializes the default random
|
||||||
|
|
@ -589,8 +590,11 @@ proc randomize*(seed: int64) {.benign.} =
|
||||||
## the same results for that seed each time.
|
## the same results for that seed each time.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `initRand proc<#initRand,int64>`_
|
## * `initRand proc<#initRand,int64>`_ that initializes a Rand state
|
||||||
|
## with a given seed
|
||||||
## * `randomize proc<#randomize>`_ that uses the current time instead
|
## * `randomize proc<#randomize>`_ that uses the current time instead
|
||||||
|
## * `initRand proc<#initRand>`_ that initializes a Rand state using
|
||||||
|
## the current time
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
from times import getTime, toUnix, nanosecond
|
from times import getTime, toUnix, nanosecond
|
||||||
|
|
||||||
|
|
@ -636,24 +640,45 @@ proc shuffle*[T](x: var openArray[T]) =
|
||||||
when not defined(nimscript) and not defined(standalone):
|
when not defined(nimscript) and not defined(standalone):
|
||||||
import times
|
import times
|
||||||
|
|
||||||
|
proc initRand(): Rand =
|
||||||
|
## Initializes a new Rand state with a seed based on the current time.
|
||||||
|
##
|
||||||
|
## The resulting state is independent of the default random number generator's state.
|
||||||
|
##
|
||||||
|
## **Note:** Does not work for NimScript or the compile-time VM.
|
||||||
|
##
|
||||||
|
## See also:
|
||||||
|
## * `initRand proc<#initRand,int64>`_ that accepts a seed for a new Rand state
|
||||||
|
## * `randomize proc<#randomize>`_ that initializes the default random
|
||||||
|
## number generator using the current time
|
||||||
|
## * `randomize proc<#randomize,int64>`_ that accepts a seed for the default
|
||||||
|
## random number generator
|
||||||
|
when defined(js):
|
||||||
|
let time = int64(times.epochTime() * 1000) and 0x7fff_ffff
|
||||||
|
result = initRand(time)
|
||||||
|
else:
|
||||||
|
let now = times.getTime()
|
||||||
|
result = initRand(convert(Seconds, Nanoseconds, now.toUnix) + now.nanosecond)
|
||||||
|
|
||||||
|
since (1, 5, 1):
|
||||||
|
export initRand
|
||||||
|
|
||||||
proc randomize*() {.benign.} =
|
proc randomize*() {.benign.} =
|
||||||
## Initializes the default random number generator with a value based on
|
## Initializes the default random number generator with a seed based on
|
||||||
## the current time.
|
## the current time.
|
||||||
##
|
##
|
||||||
## This proc only needs to be called once, and it should be called before
|
## This proc only needs to be called once, and it should be called before
|
||||||
## the first usage of procs from this module that use the default random
|
## the first usage of procs from this module that use the default random
|
||||||
## number generator.
|
## number generator.
|
||||||
##
|
##
|
||||||
## **Note:** Does not work for NimScript.
|
## **Note:** Does not work for NimScript or the compile-time VM.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `randomize proc<#randomize,int64>`_ that accepts a seed
|
## * `randomize proc<#randomize,int64>`_ that accepts a seed
|
||||||
## * `initRand proc<#initRand,int64>`_
|
## * `initRand proc<#initRand>`_ that initializes a Rand state using
|
||||||
when defined(js):
|
## the current time
|
||||||
let time = int64(times.epochTime() * 1000) and 0x7fff_ffff
|
## * `initRand proc<#initRand,int64>`_ that initializes a Rand state
|
||||||
randomize(time)
|
## with a given seed
|
||||||
else:
|
state = initRand()
|
||||||
let now = times.getTime()
|
|
||||||
randomize(convert(Seconds, Nanoseconds, now.toUnix) + now.nanosecond)
|
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue