-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
71 lines (52 loc) · 1.75 KB
/
model.py
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
import numpy as np
import json
def riffle(deck: list[int]) -> list[int]:
x = np.sort(np.random.uniform(low=0, high=1, size=len(deck)))
y = 2 * x % 1
return [deck[i] for i in np.argsort(y)]
def strip(deck: list[int]) -> list[int]:
n = len(deck)
shuffled_deck = []
while len(deck) > 0:
slice = np.random.binomial(n, 0.25)
shuffled_deck = deck[:slice] + shuffled_deck
deck = deck[slice:]
return shuffled_deck
def cut(deck: list[int]) -> list[int]:
slice = np.random.binomial(len(deck), 0.5)
return deck[slice:] + deck[:slice]
def shuffle(deck: list[int], steps: list[callable]) -> list[int]:
for step in steps:
deck = step(deck)
return deck
shuffle_sequences = {
"Single Riffle": [riffle],
"Two Riffles": [riffle, riffle],
"Three Riffles": [riffle, riffle, riffle],
"Four Riffles": [riffle, riffle, riffle, riffle],
"RRS": [riffle, riffle, strip],
"RSR": [riffle, strip, riffle],
"Strip": [strip],
"Cut": [cut],
"RRSRC": [riffle, riffle, strip, riffle, cut],
"RSRC": [riffle, strip, riffle, cut],
}
def run_experiment(repeats: int, steps: list[callable]):
results = []
for _ in range(0, repeats):
deck = list(range(1, 53))
deck = shuffle(deck, steps)
results.append(deck)
return [idx for sim in results for idx, c in enumerate(sim) if c == 1]
def simulation():
results = []
repeats = 300
for key, sequence in shuffle_sequences.items():
results.append({"key": key, "values": run_experiment(repeats, sequence)})
return results
data = simulation()
# Serializing json
json_object = json.dumps(data)
# Writing to sample.json
with open("./src/data.json", "w") as outfile:
outfile.write(json_object)