diff options
author | Simon Hafner <hafnersimon@gmail.com> | 2012-09-05 17:01:39 -0500 |
---|---|---|
committer | Simon Hafner <hafnersimon@gmail.com> | 2012-09-06 16:42:53 -0500 |
commit | 33cabeb04d4dc00f4edf40096f790b510f915dd9 (patch) | |
tree | 5171ab914a444171508d6c698129ffb7d96c81a6 /lib/pure/math.nim | |
parent | 3a1a1976645ac9ad2c449b0afea57c7c154ec37f (diff) | |
download | Nim-33cabeb04d4dc00f4edf40096f790b510f915dd9.tar.gz |
+ random number generator for floats and slices - with tests
I also took the liberty to unify the API between ECMAScript and C.
Diffstat (limited to 'lib/pure/math.nim')
-rwxr-xr-x | lib/pure/math.nim | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 587671165..d20fbc2ca 100755 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -123,17 +123,22 @@ proc variance*(x: openarray[float]): float {.noSideEffect.} = result = result + diff*diff result = result / toFloat(len(x)) -when not defined(ECMAScript): - proc random*(max: int): int - ## returns a random number in the range 0..max-1. The sequence of - ## random number is always the same, unless `randomize` is called - ## which initializes the random number generator with a "random" - ## number, i.e. a tickcount. - proc randomize*() - ## initializes the random number generator with a "random" - ## number, i.e. a tickcount. Note: Does nothing for the ECMAScript target, - ## as ECMAScript does not support this. +proc random*(max: int): int + ## returns a random number in the range 0..max-1. The sequence of + ## random number is always the same, unless `randomize` is called + ## which initializes the random number generator with a "random" + ## number, i.e. a tickcount. +proc random*(max: float): float + ## returns a random number in the range 0..<max. The sequence of + ## random number is always the same, unless `randomize` is called + ## which initializes the random number generator with a "random" + ## number, i.e. a tickcount. +proc randomize*() + ## initializes the random number generator with a "random" + ## number, i.e. a tickcount. Note: Does nothing for the ECMAScript target, + ## as ECMAScript does not support this. +when not defined(ECMAScript): proc sqrt*(x: float): float {.importc: "sqrt", header: "<math.h>".} ## computes the square root of `x`. @@ -179,10 +184,19 @@ when not defined(ECMAScript): # C procs: proc gettime(dummy: ptr cint): cint {.importc: "time", header: "<time.h>".} proc srand(seed: cint) {.importc: "srand", nodecl.} + proc srand48(seed: cint) {.importc: "srand48", nodecl.} proc rand(): cint {.importc: "rand", nodecl.} + proc drand48(): float {.importc: "drand48", nodecl.} - proc randomize() = srand(gettime(nil)) - proc random(max: int): int = return int(rand()) mod max + var uniqueCounter: int = 0 + proc randomize() = + srand(gettime(nil) + cint(uniqueCounter)) + srand48(gettime(nil) + cint(uniqueCounter)) + atomicInc(uniqueCounter) + proc random(max: int): int = + result = int(rand()) mod max + proc random(max: float): float = + result = drand48() * max proc trunc*(x: float): float {.importc: "trunc", nodecl.} proc floor*(x: float): float {.importc: "floor", nodecl.} @@ -194,8 +208,11 @@ else: proc mathrandom(): float {.importc: "Math.random", nodecl.} proc floor*(x: float): float {.importc: "Math.floor", nodecl.} proc ceil*(x: float): float {.importc: "Math.ceil", nodecl.} - proc random*(max: int): int = return int(floor(mathrandom() * float(max))) - proc randomize*() = nil + proc random(max: int): int = + result = int(floor(mathrandom() * float(max))) + proc random(max: float): float = + result = float(mathrandom() * float(max)) + proc randomize() = nil proc sqrt*(x: float): float {.importc: "Math.sqrt", nodecl.} proc ln*(x: float): float {.importc: "Math.log", nodecl.} @@ -235,6 +252,9 @@ else: 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 = + result = random(x.b - x.a) + x.a + type TRunningStat* {.pure,final.} = object ## an accumulator for statistical data n*: int ## number of pushed data |