forked from andrepputra/benchmarking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (104 loc) · 2.56 KB
/
main.go
File metadata and controls
121 lines (104 loc) · 2.56 KB
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
package main
import (
"math/rand"
"regexp"
"runtime"
"time"
)
const (
numberOfSimulation = 16
numberOfInteraction = 100
dropRate = 0.1
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
func main() {
SimulateLootRNG()
}
func SimulateLootRNG() {
rand.Seed(time.Now().UnixNano())
rx := regexp.MustCompile(`(?i)(.*)v(.*)|(.*)i(.*)|(.*)c(.*)|(.*)t(.*)|(.*)o(.*)|(.*)r(.*)|(.*)y(.*)`)
nCPU := runtime.NumCPU()
rngTests := make([]chan []int, nCPU)
for i := range rngTests {
c := make(chan []int)
//divide per CPU thread
go newSimulateRNG(numberOfSimulation/nCPU, c, rx)
rngTests[i] = c
}
// Concatentate the test results
results := make([]int, numberOfSimulation)
for i, c := range rngTests {
start := (numberOfSimulation / nCPU) * i
stop := (numberOfSimulation / nCPU) * (i + 1)
copy(results[start:stop], <-c)
}
// fmt.Println("RNG Loot Results: ", results)
}
/*
Simulates a single interaction with a monster
Returns 1 if the monster dropped an item and 0 otherwise
But if monster name doesn't contain any of character from `victory`, it will be treated as 0
*/
func interaction(rx *regexp.Regexp) int {
monsterName := String(RandomNumber())
nameContainsVictory := rx.MatchString(monsterName)
isItemDrop := rand.Float64() <= dropRate
if !nameContainsVictory {
return 0
}
if isItemDrop {
return 1
}
return 0
}
/**
* Runs several interactions and retuns a slice representing the results
*/
// func simulation(n int) []int {
// interactions := make([]int, n)
// for i := range interactions {
// interactions[i] = interaction()
// }
// return interactions
// }
func newSimulation(n int, rx *regexp.Regexp) int {
sum := 0
for i := 0; i < n; i++ {
sum += interaction(rx)
}
return sum
}
/**
* Runs several simulations and returns the results
*/
// func simulateRNG(n int, c chan []int) {
// simulations := make([]int, n)
// for i := range simulations {
// for _, v := range simulation(numberOfInteraction) {
// simulations[i] += v
// }
// }
// c <- simulations
// }
func newSimulateRNG(n int, c chan []int, rx *regexp.Regexp) {
simulations := make([]int, n)
for i := range simulations {
simulations[i] = newSimulation(numberOfInteraction, rx)
}
c <- simulations
}
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
func String(length int) string {
return StringWithCharset(length, charset)
}
func RandomNumber() int {
min := 10
max := 30
return rand.Intn(max-min+1) + min
}