Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, strutils
- proc roll(num=1, faces=6, target=6, pierce=true, explode=true): int =
- if target > faces:
- return 0
- for n in 1..num:
- let r = random(faces) + 1
- if r >= target:
- inc(result)
- if explode and r == faces:
- result += roll(1, faces, target, pierce, explode)
- elif pierce and r == 1:
- dec(result)
- const
- Rolls = 1_000_000
- Range = 100 # Static array range
- Precision = 4 # Probably don't need any lower
- N = 6 # Dice count
- D = 6 # Face count
- var
- results: array[-Range..Range, int]
- randomize()
- for i in 1..Rolls:
- let r = roll(N, D)
- inc(results[r])
- var lowest, highest: int
- for i in countup(low(results), high(results)):
- if results[i] > 0:
- lowest = i
- break
- for i in countdown(high(results), low(results)):
- if results[i] > 0:
- highest = i
- break
- let total = sum(results)
- var percentage: array[-Range..Range, float]
- for i in lowest..highest:
- percentage[i] = 100.0 * results[i].float / total.float
- for i in lowest..highest:
- let fmt = percentage[i].formatFloat(ffDecimal, Precision)
- echo "", spaces(($Range).len-($i).len), $i, ": ",
- spaces(Precision + 3 - fmt.len), fmt, " ",
- repeat('#', percentage[i].round)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement