-
Notifications
You must be signed in to change notification settings - Fork 2
/
random-path.ts
72 lines (55 loc) · 1.22 KB
/
random-path.ts
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
// Makes a random path in 1D, 2D, or 3D.
export function randomPath1d(steps: number) {
let x = 0
for (let i = 0; i < steps; i++) {
if (Math.random() < 0.5) x += 1
else x -= 1
if (x == 0) return true
}
return false
}
export function randomPath2d(steps: number) {
let x = 0
let y = 0
for (let i = 0; i < steps; i++) {
const val = Math.random()
if (val < 0.25) x += 1
else if (val < 0.5) x -= 1
else if (val < 0.75) y += 1
else y -= 1
if (x == 0 && y == 0) return true
}
return false
}
export function randomPath3d(steps: number) {
let x = 0
let y = 0
let z = 0
for (let i = 0; i < steps; i++) {
const val = Math.random()
if (val < 1 / 6) x += 1
else if (val < 1 / 3) x -= 1
else if (val < 0.5) y += 1
else if (val < 2 / 3) y -= 1
else if (val < 5 / 6) z += 1
else z -= 1
if (x == 0 && y == 0 && z == 0) return true
}
return false
}
export function repeat(
path: (steps: number) => boolean,
steps: number,
attempts: number,
) {
let successes = 0
let failures = 0
for (let i = 0; i < attempts; i++) {
if (path(steps)) {
successes++
} else {
failures++
}
}
return successes / (successes + failures)
}