commit
df04150c9c
2 changed files with 82 additions and 14 deletions
|
|
@ -123,17 +123,22 @@ proc variance*(x: openarray[float]): float {.noSideEffect.} =
|
||||||
result = result + diff*diff
|
result = result + diff*diff
|
||||||
result = result / toFloat(len(x))
|
result = result / toFloat(len(x))
|
||||||
|
|
||||||
when not defined(ECMAScript):
|
proc random*(max: int): int
|
||||||
proc random*(max: int): int
|
## returns a random number in the range 0..max-1. The sequence of
|
||||||
## returns a random number in the range 0..max-1. The sequence of
|
## random number is always the same, unless `randomize` is called
|
||||||
## random number is always the same, unless `randomize` is called
|
## which initializes the random number generator with a "random"
|
||||||
## which initializes the random number generator with a "random"
|
## number, i.e. a tickcount.
|
||||||
## number, i.e. a tickcount.
|
proc random*(max: float): float
|
||||||
proc randomize*()
|
## returns a random number in the range 0..<max. The sequence of
|
||||||
## initializes the random number generator with a "random"
|
## random number is always the same, unless `randomize` is called
|
||||||
## number, i.e. a tickcount. Note: Does nothing for the ECMAScript target,
|
## which initializes the random number generator with a "random"
|
||||||
## as ECMAScript does not support this.
|
## 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>".}
|
proc sqrt*(x: float): float {.importc: "sqrt", header: "<math.h>".}
|
||||||
## computes the square root of `x`.
|
## computes the square root of `x`.
|
||||||
|
|
||||||
|
|
@ -179,10 +184,18 @@ when not defined(ECMAScript):
|
||||||
# C procs:
|
# C procs:
|
||||||
proc gettime(dummy: ptr cint): cint {.importc: "time", header: "<time.h>".}
|
proc gettime(dummy: ptr cint): cint {.importc: "time", header: "<time.h>".}
|
||||||
proc srand(seed: cint) {.importc: "srand", nodecl.}
|
proc srand(seed: cint) {.importc: "srand", nodecl.}
|
||||||
|
proc srand48(seed: cint) {.importc: "srand48", nodecl.}
|
||||||
proc rand(): cint {.importc: "rand", nodecl.}
|
proc rand(): cint {.importc: "rand", nodecl.}
|
||||||
|
proc drand48(): float {.importc: "drand48", nodecl.}
|
||||||
|
|
||||||
proc randomize() = srand(gettime(nil))
|
proc randomize() =
|
||||||
proc random(max: int): int = return int(rand()) mod max
|
let x = gettime(nil)
|
||||||
|
srand(x)
|
||||||
|
srand48(x)
|
||||||
|
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 trunc*(x: float): float {.importc: "trunc", nodecl.}
|
||||||
proc floor*(x: float): float {.importc: "floor", nodecl.}
|
proc floor*(x: float): float {.importc: "floor", nodecl.}
|
||||||
|
|
@ -194,8 +207,11 @@ else:
|
||||||
proc mathrandom(): float {.importc: "Math.random", nodecl.}
|
proc mathrandom(): float {.importc: "Math.random", nodecl.}
|
||||||
proc floor*(x: float): float {.importc: "Math.floor", nodecl.}
|
proc floor*(x: float): float {.importc: "Math.floor", nodecl.}
|
||||||
proc ceil*(x: float): float {.importc: "Math.ceil", nodecl.}
|
proc ceil*(x: float): float {.importc: "Math.ceil", nodecl.}
|
||||||
proc random*(max: int): int = return int(floor(mathrandom() * float(max)))
|
proc random(max: int): int =
|
||||||
proc randomize*() = nil
|
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 sqrt*(x: float): float {.importc: "Math.sqrt", nodecl.}
|
||||||
proc ln*(x: float): float {.importc: "Math.log", nodecl.}
|
proc ln*(x: float): float {.importc: "Math.log", nodecl.}
|
||||||
|
|
@ -235,6 +251,9 @@ else:
|
||||||
proc `mod`*(x, y: float): float =
|
proc `mod`*(x, y: float): float =
|
||||||
result = if y == 0.0: x else: x - y * (x/y).floor
|
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
|
type
|
||||||
TRunningStat* {.pure,final.} = object ## an accumulator for statistical data
|
TRunningStat* {.pure,final.} = object ## an accumulator for statistical data
|
||||||
n*: int ## number of pushed data
|
n*: int ## number of pushed data
|
||||||
|
|
|
||||||
49
tests/run/tmath.nim
Normal file
49
tests/run/tmath.nim
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import math
|
||||||
|
import unittest
|
||||||
|
import sets
|
||||||
|
|
||||||
|
suite "random int":
|
||||||
|
test "there might be some randomness":
|
||||||
|
var set = initSet[int](128)
|
||||||
|
randomize()
|
||||||
|
for i in 1..1000:
|
||||||
|
incl(set, random(high(int)))
|
||||||
|
check len(set) == 1000
|
||||||
|
test "single number bounds work":
|
||||||
|
randomize()
|
||||||
|
var rand: int
|
||||||
|
for i in 1..1000:
|
||||||
|
rand = random(1000)
|
||||||
|
check rand < 1000
|
||||||
|
check rand > -1
|
||||||
|
test "slice bounds work":
|
||||||
|
randomize()
|
||||||
|
var rand: int
|
||||||
|
for i in 1..1000:
|
||||||
|
rand = random(100..1000)
|
||||||
|
check rand < 1000
|
||||||
|
check rand >= 100
|
||||||
|
|
||||||
|
suite "random float":
|
||||||
|
# Enable this once #197 has been resolved
|
||||||
|
# test "there might be some randomness":
|
||||||
|
# var set = initSet[float](128)
|
||||||
|
# for i in 1..10:
|
||||||
|
# for j in 1..10:
|
||||||
|
# randomize()
|
||||||
|
# incl(set, random(1.0))
|
||||||
|
# check len(set) == 100
|
||||||
|
test "single number bounds work":
|
||||||
|
randomize()
|
||||||
|
var rand: float
|
||||||
|
for i in 1..1000:
|
||||||
|
rand = random(1000.0)
|
||||||
|
check rand < 1000.0
|
||||||
|
check rand > -1.0
|
||||||
|
test "slice bounds work":
|
||||||
|
randomize()
|
||||||
|
var rand: float
|
||||||
|
for i in 1..1000:
|
||||||
|
rand = random(100.0..1000.0)
|
||||||
|
check rand < 1000.0
|
||||||
|
check rand >= 100.0
|
||||||
Loading…
Add table
Add a link
Reference in a new issue