+ random number generator for floats and slices - with tests

I also took the liberty to unify the API between ECMAScript and C.
This commit is contained in:
Simon Hafner 2012-09-05 17:01:39 -05:00
commit 33cabeb04d
2 changed files with 84 additions and 14 deletions

50
tests/run/tmath.nim Normal file
View file

@ -0,0 +1,50 @@
import math
import unittest
import sets
suite "random int":
test "there might be some randomness":
var set = initSet[int](128)
for i in 1..10:
for j in 1..10:
randomize()
incl(set, random(high(int)))
check len(set) == 100
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