diff options
author | Amjad Ben Hedhili <amjadhedhili@outlook.com> | 2022-07-16 21:51:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-16 16:51:27 -0400 |
commit | cf78c02b70ed105a173cce956ffab90bd57468a1 (patch) | |
tree | d1d3dc47b18231ee79c24077c16b240f4eb4f6ad /lib/pure | |
parent | 60dd38c50262774cadb1d365e649696380c0595e (diff) | |
download | Nim-cf78c02b70ed105a173cce956ffab90bd57468a1.tar.gz |
Make `random.rand` work with `Ordinal` (#20043)
* Make `random.rand` work with `Ordinal` * Add changelog entry * It's fine to cast to char
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/random.nim | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/lib/pure/random.nim b/lib/pure/random.nim index b8aeb86e0..832d54e3d 100644 --- a/lib/pure/random.nim +++ b/lib/pure/random.nim @@ -353,8 +353,8 @@ proc rand*[T: Ordinal or SomeFloat](x: HSlice[T, T]): T = result = rand(state, x) -proc rand*[T: SomeInteger](t: typedesc[T]): T = - ## Returns a random integer in the range `low(T)..high(T)`. +proc rand*[T: Ordinal](t: typedesc[T]): T = + ## Returns a random Ordinal in the range `low(T)..high(T)`. ## ## If `randomize <#randomize>`_ has not been called, the sequence of random ## 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 runnableExamples: randomize(567) + type E = enum a, b, c, d if false: # implementation defined - assert rand(int8) == -42 - assert rand(uint32) == 578980729'u32 - assert rand(range[1..16]) == 11 + assert rand(E) in a..d + assert rand(char) in low(char)..high(char) + 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, # 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)) else: result = cast[T](state.next) |