-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.go
160 lines (138 loc) · 4.33 KB
/
simulate.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"flag"
"fmt"
"time"
"github.com/pmarkus/shuffler/deck"
"github.com/pmarkus/shuffler/shuffle"
"github.com/pmarkus/shuffler/test"
)
func main() {
runDemo := flag.Bool("demo", false, "Run a small display of functionality.")
hardRandom := flag.Bool(
"hard",
false,
"Include a \"hard random\" (near perfect random) result at the end. Defaults to false.",
)
onlyCustom := flag.Bool(
"custom",
false,
"Show only result from the custom shuffling scheme. Defaults to false.",
)
deckSize := flag.Int(
"cards",
52,
"The amount of cards in the deck to be shuffled. Defaults to 52.",
)
shuffleTimes := flag.Int(
"shuffles",
9,
"The amount of times to shuffle the deck for each shuffling scheme. Defaults to 9.",
)
simulationIterations := flag.Int(
"iterations",
100_000,
"The amount of iterations to run the simulation before gathering results. Defaults to 100,000.",
)
flag.Parse()
if *runDemo {
fmt.Println("Demo of a deck and its interactions")
demo()
} else {
simulate(*deckSize, *shuffleTimes, *simulationIterations, *onlyCustom, *hardRandom)
}
}
func simulate(deckSize, shuffleTimes, simulationIterations int, onlyCustomTest, hardRandomTest bool) {
startT := time.Now().UnixNano()
fmt.Printf("Deck Size:\t\t%d\n", deckSize)
fmt.Printf("Shuffle iterations:\t%d\n", shuffleTimes)
fmt.Printf("Simulation iterations:\t%d\n", simulationIterations)
fmt.Println("")
// Custom shuffling scheme test
tp := test.NewTestProcessor()
// Riffle shuffle test
rstp := test.NewTestProcessor()
// Riffle shuffle test with cutting - swapping top and bottom halves - halfway through.
rswctp := test.NewTestProcessor()
// Hard random test. I.e. as close to perfect random as we can get.
hrtp := test.NewTestProcessor()
for i := 0; i < simulationIterations; i++ {
// Custom algoritm
d := deck.NewDeck(deckSize)
tp.StartIteration(*d)
d = shuffle.TwoPileRotationShuffle(d, shuffleTimes)
tp.FinishIteration(*d)
// Normal riffle shuffle on whole deck
if !onlyCustomTest {
rsd := deck.NewDeck(deckSize)
rstp.StartIteration(*rsd)
for i := 0; i < shuffleTimes; i++ {
rsd.RiffleShuffle()
}
rstp.FinishIteration(*rsd)
}
if !onlyCustomTest {
// Riffle shuffle deck, cutting halfway through all shuffles
rswcd := deck.NewDeck(deckSize)
rswctp.StartIteration(*rswcd)
for i := 0; i < shuffleTimes/2; i++ {
rswcd.RiffleShuffle()
}
top, bot := rswcd.Cut()
rswcd = deck.Stack(bot, top)
for i := int(shuffleTimes / 2); i < shuffleTimes; i++ {
rswcd.RiffleShuffle()
}
rswctp.FinishIteration(*rswcd)
}
// Hard random deck
if hardRandomTest {
hrd := deck.NewDeck(deckSize)
hrtp.StartIteration(*hrd)
hrd = deck.NewRandomDeck(deckSize)
hrtp.FinishIteration(*hrd)
}
}
// Custom algoritm
fmt.Printf("\nCustom shuffled deck\n")
fmt.Printf("Position change test:\n%s\n\n", test.PositionChangeTest(tp))
fmt.Printf("Position occurence test:\n%s\n", test.PositionOccurenceTest(tp))
// Normal riffle shuffle on whole deck
if !onlyCustomTest {
fmt.Printf("\nNormal riffle shuffled deck\n")
fmt.Printf("Position change test:\n%s\n\n", test.PositionChangeTest(rstp))
fmt.Printf("Position occurence test:\n%s\n", test.PositionOccurenceTest(rstp))
}
// Riffle shuffle deck, cutting halfway through all shuffles
if !onlyCustomTest {
fmt.Printf("\nRiffle shuffled deck, with a cut halfway through\n")
fmt.Printf("Position change test:\n%s\n\n", test.PositionChangeTest(rswctp))
fmt.Printf("Position occurence test:\n%s\n", test.PositionOccurenceTest(rswctp))
}
if hardRandomTest {
// Hard random deck
fmt.Printf("\nHard random deck\n")
fmt.Printf("Position change test:\n%s\n\n", test.PositionChangeTest(hrtp))
fmt.Printf("Position occurence test:\n%s\n", test.PositionOccurenceTest(hrtp))
}
endT := time.Now().UnixNano()
fmt.Println("")
fmt.Printf("Elapsed milliseconds: %v\n", ((endT - startT) / 1000000))
}
func demo() {
d := deck.NewDeck(8)
fmt.Printf("d:\t%s\n", d)
fmt.Println("== Split ==")
dTop, dBot := d.Cut()
fmt.Printf("dTop:\t%s\n", dTop)
fmt.Printf("dBot:\t%s\n", dBot)
fmt.Printf("d:\t%s\n", d)
fmt.Println("== Stack ==")
d = deck.Stack(dTop, dBot)
fmt.Printf("dTop:\t%s\n", dTop)
fmt.Printf("dBot:\t%s\n", dBot)
fmt.Printf("d:\t%s\n", d)
fmt.Println("== Riffle Shuffle ==")
d.RiffleShuffle()
fmt.Printf("d:\t%s\n", d)
}