blob: f8797944ee50b68401212f0a85fd30581957cb1a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
discard """
joinable: false
targets: "c js"
"""
import std/[random, stats]
randomize(233)
proc main =
var occur: array[1000, int]
for i in 0..100_000:
let x = rand(high(occur))
inc occur[x]
doAssert max(occur) <= 140 and min(occur) >= 60 # gives some slack
var a = [0, 1]
shuffle(a)
doAssert a in [[0,1], [1,0]]
doAssert rand(0) == 0
doAssert sample("a") == 'a'
when compileOption("rangeChecks"):
doAssertRaises(RangeDefect):
discard rand(-1)
doAssertRaises(RangeDefect):
discard rand(-1.0)
# don't use causes integer overflow
doAssert compiles(rand[int](low(int) .. high(int)))
main()
import math
block:
when not defined(js):
doAssert almostEqual(rand(12.5), 4.012897747078944)
doAssert almostEqual(rand(2233.3322), 879.702755321298)
type DiceRoll = range[0..6]
doAssert rand(DiceRoll).int == 4
var rs: RunningStat
for j in 1..5:
for i in 1 .. 100_000:
rs.push(gauss())
doAssert abs(rs.mean-0) < 0.08, $rs.mean
doAssert abs(rs.standardDeviation()-1.0) < 0.1
let bounds = [3.5, 5.0]
for a in [rs.max, -rs.min]:
doAssert a >= bounds[0] and a <= bounds[1]
rs.clear()
block:
type DiceRoll = range[3..6]
var flag = false
for i in 0..<100:
if rand(5.DiceRoll) < 3:
flag = true
doAssert flag # because of: rand(max: int): int
|