3 Commits

3 changed files with 112 additions and 36 deletions

View File

@ -1,5 +1,5 @@
import algorithm, random, sugar
import fixedseq, game
import fastrand, fixedseq, game
proc nextPermutation(x: var FixedSeq): bool =
@ -93,4 +93,4 @@ proc randomFuture*(dice: FixedSeq, r: var Rand): FixedSeq[5, Die, int8] =
result.initFixedSeq
let order = dice.dup(shuffle(r))
for i, color in order:
result.add((color, r.rand(1..3)))
result.add((color, r.fastRand(1..3)))

74
fastrand.nim Normal file
View File

@ -0,0 +1,74 @@
import random, math
import times, std/monotimes, strformat, strutils
proc formatNum(n: SomeNumber): string =
let s = $(n.round)
let t = s[0 .. s.len - 3]
var count = 1
for i in countdown(t.high, 0):
result.insert($t[i], 0)
if count mod 3 == 0 and i != 0:
result.insert(",", 0)
count += 1
proc formatRate(n: Natural, d: Duration): string =
result = formatNum(1_000_000'f64 * n.float64 / d.inMicroseconds.float64)
const upperBound = uint64(uint32.high)
proc fastRand*[T: Natural](r: var Rand, x: T): T =
# Nim ranges are usually inclusive, but this algorithm is exclusive
let x = x.uint64 + 1
let num = if x <= upperBound:
((r.next shr 32) * x.uint64) shr 32
else:
r.next mod x.uint64
result = T(num)
proc fastRand*(r: var Rand; x, y: Natural): Natural =
let lim = (y - x)
result = fastRand(r, lim) + x
proc fastRand*[T](r: var Rand, slice: HSlice[T, T]): T =
let n = fastRand(r, slice.a.Natural, slice.b.Natural)
result = T(n)
proc testFastRand(num = 1_000_000_000): Duration =
var r = initRand(rand(int64))
let start = getMonoTime()
for i in 1 .. num:
discard r.fastRand(5)
result = getMonoTime() - start
# echo "fastrand execution rate: ", 1000 * num / dur.inMilliseconds.int, " generated per second."
proc testStdRand(num = 1_000_000_000): Duration =
var r = initRand(rand(int64))
let start = getMonoTime()
for i in 1 .. num:
discard r.rand(4)
result = getMonoTime() - start
# echo "std rand execution rate: ", 1000 * num / dur.inMilliseconds.int, " generated per second."
when isMainModule:
randomize()
var r = initRand(rand(int64))
let runs = 100_000_000
var totals: array[5..9, int]
for i in 1 .. runs:
let n = r.fastRand(5..9)
totals[n] += 1
echo totals
# let fr = testFastRand(runs)
# echo "fastrand execution rate: ", formatNum(1_000_000 * runs / fr.inMicroseconds.int)
# let sr = testStdRand(runs)
# echo "standard execution rate: ", formatNum(1_000_000 * runs / sr.inMicroseconds.int)

View File

@ -1,7 +1,32 @@
import math, random, strformat, times
import math, random, strformat, times, std/monotimes
import fixedseq, game, simulation, ui
type
TestResults = object
ops: int
time: Duration
proc summarize(tr: TestResults) =
let secs = tr.time.inMilliseconds.float / 1000
stdout.write("Test completed:\n")
stdout.write(" " & $tr.ops, " operations in " & $round(secs, 2) & " seconds\n")
stdout.write(" " & $round(tr.ops.float / secs, 2) & " operations per second")
stdout.flushFile()
template executionTime(body: untyped): Duration =
let start = getMonoTime()
body
getMonoTime() - start
proc getRand(): Rand =
randomize()
result = initRand(rand(int64))
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
for c in Color:
let v = r.rand(1..3)
@ -18,18 +43,15 @@ proc newRandomGame(r: var Rand): Board =
result.setState(dice, [])
proc testGames(n: SomeInteger = 100): auto =
var r = initRand(rand(int64))
let dice = randomDice(r)
var b: Board
b.init
b.setState(dice, [])
b.display(1, 5)
let startTime = cpuTime()
let scores = b.randomGames(n)
result = cpuTime() - startTime
scores.display()
proc games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
var r = getRand()
var scores: ScoreSet
for i in 1 .. nTests:
let b = newRandomGame(r)
let dur = executionTime:
let s = b.randomGames(nSamples, parallel = parallel)
result.ops += s.sum()
result.time += dur
proc testLegs(n: Natural = 100): auto =
@ -72,24 +94,4 @@ proc testSpread(nTests, nSamples: Natural) =
when isMainModule:
randomize()
var r = initRand(rand(int64))
let b = newRandomGame(r)
b.display(1, 5)
echo b.showSpaces(1, 16)
let scores = b.getLegScores
echo scores.showPercents
# let start_states = 2_000
# let executionTime = testLegs(start_states)
# echo "Execution time: ", executionTime
# echo "Leg simulations per second: ", float(start_states * 29_160) / executionTime
# for i in 1 .. 1:
# let num_games = 100_000_005
# let executionTime = testGames(num_games)
# echo "Execution time: ", executionTime
# echo "Full-game simulations per second: ", float(num_games) / executionTime
# echo ""
# testSpread(100, 1_000_000)
games(10, 10_000_000).summarize()