Make random.rand work with Ordinal (#20043)
* Make `random.rand` work with `Ordinal` * Add changelog entry * It's fine to cast to char
This commit is contained in:
parent
60dd38c502
commit
cf78c02b70
2 changed files with 10 additions and 6 deletions
|
|
@ -39,6 +39,7 @@ becomes an alias for `addr`.
|
||||||
- `std/smtp` sends `ehlo` first. If the mail server does not understand, it sends `helo` as a fallback.
|
- `std/smtp` sends `ehlo` first. If the mail server does not understand, it sends `helo` as a fallback.
|
||||||
- Changed mimedb to use an `OrderedTable` instead of `OrderedTableRef`, to use it in a const.
|
- Changed mimedb to use an `OrderedTable` instead of `OrderedTableRef`, to use it in a const.
|
||||||
- `strutils.find` now use and default to `last=-1` for whole string searches, making limiting it to just the first char (`last=0`) valid.
|
- `strutils.find` now use and default to `last=-1` for whole string searches, making limiting it to just the first char (`last=0`) valid.
|
||||||
|
- `random.rand` now works with `Ordinal`s.
|
||||||
|
|
||||||
[//]: # "Additions:"
|
[//]: # "Additions:"
|
||||||
- Added `IsoWeekRange`, a range type to represent the number of weeks in an ISO week-based year.
|
- Added `IsoWeekRange`, a range type to represent the number of weeks in an ISO week-based year.
|
||||||
|
|
|
||||||
|
|
@ -353,8 +353,8 @@ proc rand*[T: Ordinal or SomeFloat](x: HSlice[T, T]): T =
|
||||||
|
|
||||||
result = rand(state, x)
|
result = rand(state, x)
|
||||||
|
|
||||||
proc rand*[T: SomeInteger](t: typedesc[T]): T =
|
proc rand*[T: Ordinal](t: typedesc[T]): T =
|
||||||
## Returns a random integer in the range `low(T)..high(T)`.
|
## Returns a random Ordinal in the range `low(T)..high(T)`.
|
||||||
##
|
##
|
||||||
## If `randomize <#randomize>`_ has not been called, the sequence of random
|
## If `randomize <#randomize>`_ has not been called, the sequence of random
|
||||||
## numbers returned from this proc will always be the same.
|
## numbers returned from this proc will always be the same.
|
||||||
|
|
@ -368,13 +368,16 @@ proc rand*[T: SomeInteger](t: typedesc[T]): T =
|
||||||
## that accepts a slice
|
## that accepts a slice
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
randomize(567)
|
randomize(567)
|
||||||
|
type E = enum a, b, c, d
|
||||||
if false: # implementation defined
|
if false: # implementation defined
|
||||||
assert rand(int8) == -42
|
assert rand(E) in a..d
|
||||||
assert rand(uint32) == 578980729'u32
|
assert rand(char) in low(char)..high(char)
|
||||||
assert rand(range[1..16]) == 11
|
assert rand(int8) in low(int8)..high(int8)
|
||||||
|
assert rand(uint32) in low(uint32)..high(uint32)
|
||||||
|
assert rand(range[1..16]) in 1..16
|
||||||
# pending csources >= 1.4.0 or fixing https://github.com/timotheecour/Nim/issues/251#issuecomment-831599772,
|
# pending csources >= 1.4.0 or fixing https://github.com/timotheecour/Nim/issues/251#issuecomment-831599772,
|
||||||
# use `runnableExamples("-r:off")` instead of `if false`
|
# use `runnableExamples("-r:off")` instead of `if false`
|
||||||
when T is range:
|
when T is range or T is enum or T is bool:
|
||||||
result = rand(state, low(T)..high(T))
|
result = rand(state, low(T)..high(T))
|
||||||
else:
|
else:
|
||||||
result = cast[T](state.next)
|
result = cast[T](state.next)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue