Created
March 8, 2018 21:56
-
-
Save DaRaFF/4e5d0f1b75428e40135d8fa187620120 to your computer and use it in GitHub Desktop.
Ice Hockey Probability to stay in the league
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = require('lodash') | |
const teams = [ | |
{ | |
id: 1, | |
name: 'Ice Dukes', | |
points: 16 | |
}, | |
{ | |
id: 2, | |
name: 'EHC Sharks', | |
points: 16 | |
}, | |
{ | |
id: 3, | |
name: 'SC Kloten Flames', | |
points: 14 | |
}, | |
{ | |
id: 4, | |
name: 'EHC Sagmäälfäger', | |
points: 14 | |
}, | |
{ | |
id: 5, | |
name: 'HC Wild Piranhas', | |
points: 12 | |
}, | |
{ | |
id: 6, | |
name: 'Another Team', | |
points: 0 | |
} | |
] | |
const games = [ | |
[5,6], [3,6], [2,6], [5,3], | |
[3,4], [5,6], | |
// [1,6], [1,6], [1,6] | |
] | |
function stayOrLeave (_teams, _games) { | |
const teams = _.cloneDeep(_teams) | |
const games = _.cloneDeep(_games) | |
const end = _.reduce(games, function(table, value, key) { | |
const randomnumber = Math.floor(Math.random() * 3) | |
if(randomnumber == 0) { | |
const winner = _.find(teams, { 'id': value[0] }) | |
winner.points +=2 | |
// console.log('winner', winner) | |
} | |
if(randomnumber == 1) { | |
const winner1 = _.find(teams, { 'id': value[0] }) | |
const winner2 = _.find(teams, { 'id': value[1] }) | |
winner1.points +=1 | |
winner2.points +=1 | |
// console.log('draw', winner1,winner2) | |
} | |
if(randomnumber == 2) { | |
const winner = _.find(teams, { 'id': value[1] }) | |
winner.points +=2 | |
// console.log('winner', winner) | |
} | |
return table | |
}, teams) | |
const orderedTable = _.orderBy(end, ['points'], ['asc']) | |
const rank = _.findIndex(orderedTable, function(o) { return o.id == 1 }) | |
// console.log(orderedTable) | |
// console.log('rank', rank+1) | |
let stay = 0 | |
let leave = 0 | |
let runs = 0 | |
if(rank+1 <= 2) { | |
leave = leave + 1 | |
// console.log('leave') | |
return 0 | |
} else { | |
stay = stay + 1 | |
// console.log('stay') | |
return 1 | |
} | |
} | |
let stay = 0 | |
let total = 0 | |
for (var i = 0; i < 100000; i++) { | |
total +=1 | |
if(stayOrLeave(teams, games)) { | |
stay +=1 | |
} | |
console.log(`Wahrscheinlichkeit Verbleib ZEP A: ${Math.floor(100/total*stay)}%`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment