mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-15 07:43:26 +00:00
Add checks for random.rand() (#8431)
This commit is contained in:
@@ -110,7 +110,7 @@ proc random*[T](a: openArray[T]): T {.deprecated.} =
|
||||
## Use ``rand`` instead.
|
||||
result = a[random(a.low..a.len)]
|
||||
|
||||
proc rand*(r: var Rand; max: int): int {.benign.} =
|
||||
proc rand*(r: var Rand; max: Natural): int {.benign.} =
|
||||
## Returns a random number in the range 0..max. The sequence of
|
||||
## random number is always the same, unless `randomize` is called
|
||||
## which initializes the random number generator with a "random"
|
||||
@@ -128,7 +128,7 @@ proc rand*(max: int): int {.benign.} =
|
||||
## number, i.e. a tickcount.
|
||||
rand(state, max)
|
||||
|
||||
proc rand*(r: var Rand; max: float): float {.benign.} =
|
||||
proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.benign.} =
|
||||
## Returns a random number in the range 0..max. The sequence of
|
||||
## random number is always the same, unless `randomize` is called
|
||||
## which initializes the random number generator with a "random"
|
||||
@@ -218,4 +218,17 @@ when isMainModule:
|
||||
doAssert rand(0) == 0
|
||||
doAssert rand("a") == 'a'
|
||||
|
||||
when compileOption("rangeChecks"):
|
||||
try:
|
||||
discard rand(-1)
|
||||
doAssert false
|
||||
except RangeError:
|
||||
discard
|
||||
|
||||
try:
|
||||
discard rand(-1.0)
|
||||
doAssert false
|
||||
except RangeError:
|
||||
discard
|
||||
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user