diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2014-08-13 21:12:12 +0100 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2014-08-13 21:12:12 +0100 |
commit | add0a0e9bc5383923eef9c764788feb810f8edc6 (patch) | |
tree | d673197603a4930cdbd574ce6f78412ec61d68b2 | |
parent | 3b7a1498e7e06576c461df9a910b3e6ba79a9feb (diff) | |
parent | fc6a799fc68092998f352ef5689eed2b7f847e1b (diff) | |
download | Nim-add0a0e9bc5383923eef9c764788feb810f8edc6.tar.gz |
Merge pull request #1351 from def-/random-openarrays
Add random() for openarrays
-rw-r--r-- | lib/pure/math.nim | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index e76e96da5..8af09114b 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -279,8 +279,13 @@ proc `mod`*(x, y: float): float = result = if y == 0.0: x else: x - y * (x/y).floor proc random*[T](x: TSlice[T]): T = + ## For a slice `a .. b` returns a value in the range `a .. b-1`. result = random(x.b - x.a) + x.a - + +proc random[T](a: openarray[T]): T = + ## returns a random element from the openarray `a`. + result = a[random(a.low..a.len)] + type TRunningStat* {.pure,final.} = object ## an accumulator for statistical data n*: int ## number of pushed data |