Calling randomize() again within 1 second will now provide a different seed
This commit is contained in:
parent
dc956c485a
commit
cabc5c80d3
2 changed files with 17 additions and 1 deletions
|
|
@ -20,6 +20,8 @@
|
||||||
when defined(Posix) and not defined(haiku):
|
when defined(Posix) and not defined(haiku):
|
||||||
{.passl: "-lm".}
|
{.passl: "-lm".}
|
||||||
|
|
||||||
|
import times
|
||||||
|
|
||||||
const
|
const
|
||||||
PI* = 3.1415926535897932384626433 ## the circle constant PI (Ludolph's number)
|
PI* = 3.1415926535897932384626433 ## the circle constant PI (Ludolph's number)
|
||||||
E* = 2.71828182845904523536028747 ## Euler's number
|
E* = 2.71828182845904523536028747 ## Euler's number
|
||||||
|
|
@ -201,7 +203,7 @@ when not defined(JS):
|
||||||
result = drand48() * max
|
result = drand48() * max
|
||||||
|
|
||||||
proc randomize() =
|
proc randomize() =
|
||||||
randomize(gettime(nil))
|
randomize(cast[int](epochTime()))
|
||||||
|
|
||||||
proc randomize(seed: int) =
|
proc randomize(seed: int) =
|
||||||
srand(cint(seed))
|
srand(cint(seed))
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,13 @@ suite "random int":
|
||||||
rand = random(100..1000)
|
rand = random(100..1000)
|
||||||
check rand < 1000
|
check rand < 1000
|
||||||
check rand >= 100
|
check rand >= 100
|
||||||
|
test "randomize() again gives new numbers":
|
||||||
|
randomize()
|
||||||
|
var rand1 = random(1000000)
|
||||||
|
randomize()
|
||||||
|
var rand2 = random(1000000)
|
||||||
|
check rand1 != rand2
|
||||||
|
|
||||||
|
|
||||||
suite "random float":
|
suite "random float":
|
||||||
test "there might be some randomness":
|
test "there might be some randomness":
|
||||||
|
|
@ -45,3 +52,10 @@ suite "random float":
|
||||||
rand = random(100.0..1000.0)
|
rand = random(100.0..1000.0)
|
||||||
check rand < 1000.0
|
check rand < 1000.0
|
||||||
check rand >= 100.0
|
check rand >= 100.0
|
||||||
|
test "randomize() again gives new numbers":
|
||||||
|
randomize()
|
||||||
|
var rand1:float = random(1000000.0)
|
||||||
|
randomize()
|
||||||
|
var rand2:float = random(1000000.0)
|
||||||
|
check rand1 != rand2
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue