-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.js
35 lines (33 loc) · 927 Bytes
/
random.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
// --------------------------------------
// Random coin flip simulation.
//
// Usage:
// $ node random.js
//
// --------------------------------------
var n = 999999;
var heads_success_rate = 0;
var heads_wins = 0;
var tails_success_rate = 0;
var tails_wins = 0;
for(var i = 0; i < n; i++) {
var x = Math.floor(Math.random() * 10);
choice = "";
if(x < 5) {
choice = "Heads";
heads_wins++;
heads_success_rate = heads_wins / i;
}
else if(x >= 5) {
choice = "Tails";
tails_wins++;
tails_success_rate = tails_wins / i;
}
else {
choice = "Something blew up";
}
console.log("\n" + choice + "\nHeads success rate: " + heads_success_rate);
console.log("Tails success rate: " + tails_success_rate);
}
document.getElementById("heads").innerHTML = heads_success_rate;
document.getElementById("tails").innerHTML = tails_success_rate;