Add shuffle to math module
This commit is contained in:
parent
232dba8f95
commit
d57d1f00cd
1 changed files with 8 additions and 0 deletions
|
|
@ -284,6 +284,14 @@ proc random[T](a: openArray[T]): T =
|
||||||
## returns a random element from the openarray `a`.
|
## returns a random element from the openarray `a`.
|
||||||
result = a[random(a.low..a.len)]
|
result = a[random(a.low..a.len)]
|
||||||
|
|
||||||
|
proc shuffle[T](a: var openArray[T]) =
|
||||||
|
## Shuffles the elements in `a`. Note that ``randomize`` needs to be called
|
||||||
|
## to initialize the random number generator, otherwise ``shuffle`` always
|
||||||
|
## shuffles in the same way.
|
||||||
|
for i in countdown(a.high, 0):
|
||||||
|
let j = random(i + 1)
|
||||||
|
swap(a[i], a[j])
|
||||||
|
|
||||||
type
|
type
|
||||||
RunningStat* = object ## an accumulator for statistical data
|
RunningStat* = object ## an accumulator for statistical data
|
||||||
n*: int ## number of pushed data
|
n*: int ## number of pushed data
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue