-
Notifications
You must be signed in to change notification settings - Fork 0
/
plinko.go
55 lines (44 loc) · 1.17 KB
/
plinko.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
package vero
import (
"math"
"strconv"
"github.com/pastc/vero/v2/internal"
)
// Plinko generates the column number that the ball landed on
//
// 0
// 0 1
// 0 1 2
// 0 1 2 3
func Plinko(serverSeed string, clientSeed string, nonce int, iteration int, rows int) (int, error) {
game := "PLINKO"
var coordinate int
// repeat it the number of rows (n)
for i := range rows {
seed := internal.GetCombinedSeed(game, clientSeed, strconv.Itoa(nonce), strconv.Itoa(iteration), strconv.Itoa(i))
hash := internal.Hmac256(serverSeed, seed)
index := 0
lucky, err := internal.GetLucky(hash, index)
if err != nil {
return 0, err
}
for lucky >= math.Pow(10, 6) {
index++
lucky, err = internal.GetLucky(hash, index)
if err != nil {
return 0, err
}
if (index*5)+5 > 128 {
return Plinko(serverSeed, clientSeed, nonce, iteration+1, rows)
}
}
luckyNumber := int(math.Floor(math.Mod(lucky, math.Pow(10, 4))))
if luckyNumber < 5000 {
coordinate -= 1
} else {
coordinate += 1
}
}
// probability math.Trunc(internal.BinomialDistribution(rows, (rows+coordinate)/2)*math.Pow(10, 6)) / math.Pow(10, 6)
return (rows + coordinate) / 2, nil
}