diff options
author | def <dennis@felsin9.de> | 2015-01-02 22:26:43 +0100 |
---|---|---|
committer | def <dennis@felsin9.de> | 2015-02-16 20:44:24 +0100 |
commit | d57d1f00cd2c4a0023250ee0506c10393605f167 (patch) | |
tree | 773b1857f1fd8cc71d1bb438ab86525f44537cb8 | |
parent | 232dba8f953704cf74cf487c43cee0495cfc49d2 (diff) | |
download | Nim-d57d1f00cd2c4a0023250ee0506c10393605f167.tar.gz |
Add shuffle to math module
-rw-r--r-- | lib/pure/math.nim | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 6b1d09b90..b2a588aa8 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -284,6 +284,14 @@ proc random[T](a: openArray[T]): T = ## returns a random element from the openarray `a`. 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 RunningStat* = object ## an accumulator for statistical data n*: int ## number of pushed data |