Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use generics in random #16283

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 15 additions & 33 deletions lib/pure/random.nim
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ proc rand*(r: var Rand; max: Natural): int {.benign.} =
## Returns a random integer in the range `0..max` using the given state.
##
## See also:
## * `rand proc<#rand,int>`_ that returns an integer using the default
## random number generator
## * `rand proc<#rand,T>`_ `T` are integers, floats, and enums without holes.
## * `rand proc<#rand,Rand,range[]>`_ that returns a float
## * `rand proc<#rand,Rand,HSlice[T,T]>`_ that accepts a slice
## * `rand proc<#rand,typedesc[T]>`_ that accepts an integer or range type
Expand All @@ -233,28 +232,6 @@ proc rand*(r: var Rand; max: Natural): int {.benign.} =
if x <= randMax - (randMax mod Ui(max)):
return int(x mod (uint64(max)+1u64))

proc rand*(max: int): int {.benign.} =
## Returns a random integer in the range `0..max`.
##
## If `randomize<#randomize>`_ has not been called, the sequence of random
## numbers returned from this proc will always be the same.
##
## This proc uses the default random number generator. Thus, it is **not**
## thread-safe.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this remark? Shouldn't we keep it?

Copy link
Member Author

@ringabout ringabout Dec 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already kept

## This proc uses the default random number generator. Thus, it is **not**

rand(max: float) already has the same comments and this generic procs reuse that commentbefore.

##
## See also:
## * `rand proc<#rand,Rand,Natural>`_ that returns an integer using a
## provided state
## * `rand proc<#rand,float>`_ that returns a float
## * `rand proc<#rand,HSlice[T,T]>`_ that accepts a slice
## * `rand proc<#rand,typedesc[T]>`_ that accepts an integer or range type
runnableExamples:
randomize(123)
doAssert rand(100) == 0
doAssert rand(100) == 96
doAssert rand(100) == 66
rand(state, max)

proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.benign.} =
## Returns a random floating point number in the range `0.0..max`
## using the given state.
Expand All @@ -276,8 +253,10 @@ proc rand*(r: var Rand; max: range[0.0 .. high(float)]): float {.benign.} =
let u = (0x3FFu64 shl 52u64) or (x shr 12u64)
result = (cast[float](u) - 1.0) * max

proc rand*(max: float): float {.benign.} =
## Returns a random floating point number in the range `0.0..max`.
proc rand*[T: Ordinal or SomeFloat](max: T): T {.benign.} =
## Returns a random floating point number in the range `T(0) .. max`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/random floating point number/random number/

##
## Allowed types for `T` are integers, floats, and enums without holes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is redundant (and only approximately correct) and should be removed, it's already specified what T is in the generated docs:

image

furthermore, Ordinal is clickable (thanks to docgen) so users who see Ordinal for the 1st time can just click to see the definition, which is explained in 1 place, where it belongs (https://nim-lang.github.io/Nim/system.html#Ordinal) => "1 definition rule" for docs

note that the definition for https://nim-lang.github.io/Nim/system.html#Ordinal should be fixed via s/enums/enums without holes/ but that's a separate issue

##
## If `randomize<#randomize>`_ has not been called, the sequence of random
## numbers returned from this proc will always be the same.
Expand All @@ -286,16 +265,21 @@ proc rand*(max: float): float {.benign.} =
## thread-safe.
##
## See also:
## * `rand proc<#rand,Rand,Natural>`_ that returns an integer using a
## provided state
## * `rand proc<#rand,Rand,range[]>`_ that returns a float using a
## provided state
## * `rand proc<#rand,int>`_ that returns an integer
## * `rand proc<#rand,HSlice[T,T]>`_ that accepts a slice
## * `rand proc<#rand,typedesc[T]>`_ that accepts an integer or range type
runnableExamples:
randomize(234)
randomize(123)
doAssert rand(100) == 0
doAssert rand(100) == 96
doAssert rand(100) == 66

let f = rand(1.0)
## f = 8.717181376738381e-07
rand(state, max)
result = T(rand(state, max))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast to avoid overflow errors


proc rand*[T: Ordinal or SomeFloat](r: var Rand; x: HSlice[T, T]): T =
## For a slice `a..b`, returns a value in the range `a..b` using the given
Expand Down Expand Up @@ -335,8 +319,7 @@ proc rand*[T: Ordinal or SomeFloat](x: HSlice[T, T]): T =
## See also:
## * `rand proc<#rand,Rand,HSlice[T,T]>`_ that accepts a slice and uses
## a provided state
## * `rand proc<#rand,int>`_ that returns an integer
## * `rand proc<#rand,float>`_ that returns a floating point number
## * `rand proc<#rand,T>`_ `T` are integers, floats, and enums without holes.
## * `rand proc<#rand,typedesc[T]>`_ that accepts an integer or range type
runnableExamples:
randomize(345)
Expand All @@ -355,8 +338,7 @@ proc rand*[T: SomeInteger](t: typedesc[T]): T =
## thread-safe.
##
## See also:
## * `rand proc<#rand,int>`_ that returns an integer
## * `rand proc<#rand,float>`_ that returns a floating point number
## * `rand proc<#rand,T>`_ `T` are integers, floats, and enums without holes.
## * `rand proc<#rand,HSlice[T,T]>`_ that accepts a slice
runnableExamples:
randomize(567)
Expand Down