Compare commits
1 Commits
fastrand
...
38e9e23dea
Author | SHA1 | Date | |
---|---|---|---|
38e9e23dea |
@ -1,5 +1,5 @@
|
|||||||
import algorithm, random, sugar
|
import algorithm, random, sugar
|
||||||
import fastrand, fixedseq, game
|
import fixedseq, game
|
||||||
|
|
||||||
|
|
||||||
proc nextPermutation(x: var FixedSeq): bool =
|
proc nextPermutation(x: var FixedSeq): bool =
|
||||||
@ -93,4 +93,4 @@ proc randomFuture*(dice: FixedSeq, r: var Rand): FixedSeq[5, Die, int8] =
|
|||||||
result.initFixedSeq
|
result.initFixedSeq
|
||||||
let order = dice.dup(shuffle(r))
|
let order = dice.dup(shuffle(r))
|
||||||
for i, color in order:
|
for i, color in order:
|
||||||
result.add((color, r.fastRand(1..3)))
|
result.add((color, r.rand(1..3)))
|
||||||
|
@ -2,4 +2,4 @@
|
|||||||
--d: release
|
--d: release
|
||||||
--opt: speed
|
--opt: speed
|
||||||
--passC: -flto
|
--passC: -flto
|
||||||
--passL: -flto
|
--passL: -flto
|
||||||
|
74
fastrand.nim
74
fastrand.nim
@ -1,74 +0,0 @@
|
|||||||
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)
|
|
22
game.nim
22
game.nim
@ -57,7 +57,7 @@ type
|
|||||||
squares*: array[1..16, Square]
|
squares*: array[1..16, Square]
|
||||||
camels*: array[Color, range[1..16]]
|
camels*: array[Color, range[1..16]]
|
||||||
diceRolled*: array[Color, bool]
|
diceRolled*: array[Color, bool]
|
||||||
leader*: Option[Color]
|
winner*: Option[Color]
|
||||||
gameOver*: bool
|
gameOver*: bool
|
||||||
initialized: bool
|
initialized: bool
|
||||||
|
|
||||||
@ -66,6 +66,10 @@ type
|
|||||||
template `[]`*[T](b: var Board, idx: T): var Square =
|
template `[]`*[T](b: var Board, idx: T): var Square =
|
||||||
b.squares[idx]
|
b.squares[idx]
|
||||||
|
|
||||||
|
# apparently we need separate ones for mutable and non-mutable
|
||||||
|
template `[]`*[T](b: Board, idx: T): Square =
|
||||||
|
b.squares[idx]
|
||||||
|
|
||||||
|
|
||||||
proc hash*(b: Board): Hash =
|
proc hash*(b: Board): Hash =
|
||||||
var h: Hash = 0
|
var h: Hash = 0
|
||||||
@ -87,6 +91,11 @@ proc init*(b: var Board) =
|
|||||||
b.initialized = true
|
b.initialized = true
|
||||||
|
|
||||||
|
|
||||||
|
proc leader*(b: Board): Color =
|
||||||
|
let leadSquare = max(b.camels)
|
||||||
|
result = b[leadSquare].camels[^1]
|
||||||
|
|
||||||
|
|
||||||
proc display*(b: Board, start, stop: int) =
|
proc display*(b: Board, start, stop: int) =
|
||||||
for i in start..stop:
|
for i in start..stop:
|
||||||
let sq = b.squares[i]
|
let sq = b.squares[i]
|
||||||
@ -108,9 +117,6 @@ proc setState*(b: var Board;
|
|||||||
for (tile, dest) in tiles:
|
for (tile, dest) in tiles:
|
||||||
b[dest].tile = some(tile)
|
b[dest].tile = some(tile)
|
||||||
|
|
||||||
let leadCamel = b[max(b.camels)].camels[^1] # top camel in the last currently-occupied space
|
|
||||||
b.leader = some(leadCamel)
|
|
||||||
|
|
||||||
|
|
||||||
proc diceRemaining*(b: Board): ColorStack =
|
proc diceRemaining*(b: Board): ColorStack =
|
||||||
result.initFixedSeq
|
result.initFixedSeq
|
||||||
@ -119,7 +125,7 @@ proc diceRemaining*(b: Board): ColorStack =
|
|||||||
|
|
||||||
|
|
||||||
proc resetDice*(b: var Board) =
|
proc resetDice*(b: var Board) =
|
||||||
for c, rolled in b.diceRolled:
|
for c in Color:
|
||||||
b.diceRolled[c] = false
|
b.diceRolled[c] = false
|
||||||
|
|
||||||
|
|
||||||
@ -130,7 +136,7 @@ proc advance*(b: var Board, die: Die) =
|
|||||||
var endPos = startPos + roll
|
var endPos = startPos + roll
|
||||||
|
|
||||||
if endPos > 16: # camel has passed the finish line
|
if endPos > 16: # camel has passed the finish line
|
||||||
b.leader = some(b[startPos].camels[^1])
|
b.winner = some(b[startPos].camels[^1])
|
||||||
b.gameOver = true
|
b.gameOver = true
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -154,8 +160,4 @@ proc advance*(b: var Board, die: Die) =
|
|||||||
for i in (dstPrevHigh + 1) .. b[endPos].camels.high:
|
for i in (dstPrevHigh + 1) .. b[endPos].camels.high:
|
||||||
b.camels[b[endPos].camels[i]] = endPos
|
b.camels[b[endPos].camels[i]] = endPos
|
||||||
|
|
||||||
# if we are stacking on or moving past the previous leader
|
|
||||||
if endPos >= b.camels[b.leader.get]:
|
|
||||||
b.leader = some(b[endPos].camels[^1])
|
|
||||||
|
|
||||||
b.diceRolled[color] = true
|
b.diceRolled[color] = true
|
@ -52,7 +52,7 @@ iterator legEndStates(b: Board): Board =
|
|||||||
|
|
||||||
proc getLegScores*(b: Board): ScoreSet =
|
proc getLegScores*(b: Board): ScoreSet =
|
||||||
for prediction in b.legEndStates:
|
for prediction in b.legEndStates:
|
||||||
inc result[prediction.leader.get]
|
inc result[prediction.winner.get]
|
||||||
|
|
||||||
|
|
||||||
# =====================
|
# =====================
|
||||||
@ -65,7 +65,7 @@ proc randomGame*(b: Board, r: var Rand): Color =
|
|||||||
for roll in randomFuture(projection.diceRemaining, r):
|
for roll in randomFuture(projection.diceRemaining, r):
|
||||||
projection.advance(roll)
|
projection.advance(roll)
|
||||||
if projection.gameOver:
|
if projection.gameOver:
|
||||||
return projection.leader.get
|
return projection.winner.get
|
||||||
projection.resetDice()
|
projection.resetDice()
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user