-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.js
33 lines (30 loc) · 791 Bytes
/
q1.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
//min included, max not included
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function game(){
for(let round = 0;;round++){
let diceA = getRandomInt(1,7);
let diceB = getRandomInt(1,7);
if(diceA + diceB === 7){
if(round%2===0)
return 0;
else
return 1;
}
}
}
function probabilityOfFirstWinUsingNGames(nGames){
let firstWins = 0;
let secondWins = 0;
for(let games = 0; games < nGames;games++){
if(game() === 0)
firstWins++;
else
secondWins++;
}
return firstWins/nGames;
}
console.log(probabilityOfFirstWinUsingNGames(100000000));