-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.js
80 lines (64 loc) · 2.55 KB
/
examples.js
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
import { hasDuplicates, mapToDistribution } from "./helpers/helpers";
import Outcomes, { printProbability } from "./src/outcomes";
; (() => {
const threeDices = Outcomes({
title: "throw dice 3 times",
isDependent: false,
mode: "compute",
range: [1, 2, 3, 4, 5, 6], // input range (eg. dice)
times: 3, // roll n times
showData: true
});
const sumGreaterThan15 = threeDices('sum greater than 15', arr => arr.reduce((a, b) => a + b) > 15)
const atLeastTwoOfAKind = threeDices('at least two of a kind', arr => hasDuplicates(arr));
const atLeastAFive = threeDices('rolling a five', arr => arr.includes(5));
const threeOfAKind = threeDices('three of a kind', arr => {
const distribution = mapToDistribution(arr);
return Object.values(distribution).includes(3)
});
printProbability(...sumGreaterThan15);
printProbability(...atLeastTwoOfAKind)
printProbability(...atLeastAFive)
printProbability(...threeOfAKind)
})();
; (() => {
const towerDefense = Outcomes({
title: "Five towers that each have a 20% probability of stopping an enemy",
isDependent: false,
mode: "compute",
range: [1, 2, 3, 4, 5],
times: 5,
showData: true
});
const oneTowerStopping = towerDefense('at least one tower stopping the enemy', arr => arr.includes(1));
printProbability(...oneTowerStopping)
})();
; (() => {
const range = Array.from({ length: 365 }, (n, i) => i + 1);
const peoplesBirthday = Outcomes({
title: "100 people with random birthdays",
isDependent: false,
mode: "simulate",
range: range,
times: 100,
simulationAccuracy: 10000,
showData: false
});
const birthdayConundrum = peoplesBirthday('at least two people having the same birthday', v => hasDuplicates(v));
printProbability(...birthdayConundrum)
})();
; (() => {
const selectingWithoutReplacement = Outcomes({
title: "Picking a subset from a collection of blue(B), green(G) and orange(O) balls without replacement",
isDependent: true,
mode: "compute",
range: ["B", "G", "G", "G", "O"],
times: 4,
showData: true
});
const oneBlueTwoGreenOneOrange = selectingWithoutReplacement('selecting 1 blue ball, 2 green balls and 1 orange ball', arr => {
const distribution = mapToDistribution(arr);
return distribution["B"] == 1 && distribution["G"] == 2 && distribution["O"] == 1
});
printProbability(...oneBlueTwoGreenOneOrange)
})();