Fixed crash in rand (#7103)
This commit is contained in:
parent
33b0e73962
commit
1b3f640188
1 changed files with 5 additions and 0 deletions
|
|
@ -115,6 +115,7 @@ proc rand*(r: var Rand; max: int): int {.benign.} =
|
||||||
## random number is always the same, unless `randomize` is called
|
## random number is always the same, unless `randomize` is called
|
||||||
## which initializes the random number generator with a "random"
|
## which initializes the random number generator with a "random"
|
||||||
## number, i.e. a tickcount.
|
## number, i.e. a tickcount.
|
||||||
|
if max == 0: return
|
||||||
while true:
|
while true:
|
||||||
let x = next(r)
|
let x = next(r)
|
||||||
if x <= randMax - (randMax mod ui(max)):
|
if x <= randMax - (randMax mod ui(max)):
|
||||||
|
|
@ -213,4 +214,8 @@ when isMainModule:
|
||||||
shuffle(a)
|
shuffle(a)
|
||||||
doAssert a[0] == 1
|
doAssert a[0] == 1
|
||||||
doAssert a[1] == 0
|
doAssert a[1] == 0
|
||||||
|
|
||||||
|
doAssert rand(0) == 0
|
||||||
|
doAssert rand("a") == 'a'
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue