Created
April 9, 2020 18:19
-
-
Save amih/a706486b48268cc905982c8aecc90332 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/sagigac
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://unpkg.com/[email protected]/lib/gaussian.js"></script> | |
<script src="https://bossanova.uk/jexcel/v3/jexcel.js"></script> | |
<script src="https://bossanova.uk/jsuites/v2/jsuites.js"></script> | |
<link rel="stylesheet" href="https://bossanova.uk/jexcel/v3/jexcel.css" type="text/css" /> | |
<link rel="stylesheet" href="https://bossanova.uk/jsuites/v2/jsuites.css" type="text/css" /> | |
<style id="jsbin-css"> | |
html, body { | |
height: 100%; | |
} | |
#left, #right { | |
height: 100%; | |
display: inline-block; | |
} | |
#left { | |
width: 200px; | |
margin-right: 60px | |
} | |
#right { | |
width: 420px; | |
} | |
#summary, #details { | |
height: 95%; | |
border: 1px solid blue; | |
overflow-y : auto; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="left"> | |
<div>Summary</div> | |
<div id="summary"></div> | |
</div> | |
<div id="right"> | |
<div>Details (of only the first few rounds)</div> | |
<div id="details"></div> | |
</div> | |
<script id="jsbin-javascript"> | |
//jshint esnext:true | |
// noprotect | |
const gaussian = window.gaussian; // require('gaussian'); | |
const N45 = (mean, sd=45) => gaussian(mean, sd*sd).ppf(Math.random()); | |
const U = (min, max) => Math.floor(Math.random()*(max - min + 1) + min) | |
const removeSmallest = (arr) => { | |
arr.sort((a, b) => Math.sign(a.bet - b.bet)); | |
const numOfLosers = arr.reduce((acc, cur) => (cur.bet == arr[0].bet) ? acc+1 : acc, 0); | |
const loser = arr.splice(U(1, numOfLosers) - 1, 1); // remove one of the losers | |
return arr; | |
} | |
var table1 = jexcel(document.getElementById('details'), { | |
data:[ [ 2, 10, 'this is just a dummy line', '=B1*C1'] ], | |
columns: [ | |
{ title: 'Round', type: 'text', width:'50px', }, | |
{ title: 'inner', type: 'text', width:'50px', }, | |
{ title: 'X1', type: 'number', width:'30px', }, | |
{ title: 'X2', type: 'number', width:'30px', }, | |
{ title: 'Player-Strategy', type: 'number', width:'200px', align: 'left'}, | |
{ title: '?', type: 'number', width:'30px', }, | |
{ title: 'Bet', type: 'number', width:'30px', }, | |
], | |
rowResize: true, | |
}); | |
var dataS = [ [ 'this dummy line will be removed at the end of the program...', 10 ] ]; | |
var tableS = jexcel(document.getElementById('summary'), { | |
data:dataS, | |
columns: [ | |
{ title: 'Player-Strategy', type: 'text', width:'150px', align: 'left'}, | |
{ title: 'wins', type: 'number', width:'50px'}, | |
], | |
rowResize: true, | |
}); | |
const getPlayers = () => { | |
let arr = [ | |
{ | |
strategy: 'simple,max(x1,x2)', | |
func: (X1, X2, T) => { | |
if(X1>X2){ | |
return 'S'; | |
} else { | |
return 'R'; | |
} | |
} | |
}, | |
{ | |
strategy: 'crazy,min(x1,x2)', | |
func: (X1, X2, T) => { | |
if(X1<X2){ | |
return 'S'; | |
} else { | |
return 'R'; | |
} | |
} | |
}, | |
{ | |
strategy: 'alwaysSafe', | |
func: (X1, X2, T) => { | |
return 'S'; | |
} | |
}, | |
{ | |
strategy: 'alwaysRisk', | |
func: (X1, X2, T) => { | |
return 'R'; | |
} | |
}, | |
]; | |
// multiply each strategy | |
let mularr = []; | |
for(item in arr){ | |
for(let i=0; i<6; i++){ | |
let temp = Object.assign({}, arr[item]); | |
temp.strategy += i; | |
mularr.push(temp); | |
} | |
} | |
return mularr; | |
} | |
var winnersList = []; | |
const main = () => { | |
for(let roundNum = 0; roundNum<10000; roundNum++){ | |
let player = getPlayers(); | |
const games = player.length; | |
while(player.length>1){ | |
let X1 = U(0,150); | |
let X2 = U(0,150); | |
for(i=0; i<player.length; i++){ | |
let decision = player[i].func(X1, X2, player.length); | |
if(decision == 'S'){ | |
player[i].bet = X1; | |
}else{ | |
player[i].bet = Math.round(N45(X2)); | |
} | |
if(roundNum<2){ | |
table1.insertRow([(roundNum+1).toString(), (games - player.length + 1).toString(), X1, X2, player[i].strategy, decision, player[i].bet]); | |
} | |
} | |
player = removeSmallest(player); // find loser and remove him | |
if(player.length==1){ | |
// tableS.insertRow([(roundNum+1).toString(), player[0].strategy]); | |
winnersList.push(player[0].strategy); | |
} | |
} | |
} | |
var winnerFrequency = {}; | |
for(let i=0; i<winnersList.length; i++){ | |
if( winnerFrequency[winnersList[i]]===undefined ){ | |
winnerFrequency[winnersList[i]] = 1; | |
}else{ | |
winnerFrequency[winnersList[i]]++; | |
} | |
} | |
for(w in winnerFrequency){ | |
tableS.insertRow([w, winnerFrequency[w]]); | |
} | |
table1.deleteRow(0,1); | |
tableS.deleteRow(0,1); | |
tableS.orderBy(1); | |
tableS.orderBy(1); | |
} | |
main(); | |
</script> | |
<script id="jsbin-source-css" type="text/css">html, body { | |
height: 100%; | |
} | |
#left, #right { | |
height: 100%; | |
display: inline-block; | |
} | |
#left { | |
width: 200px; | |
margin-right: 60px | |
} | |
#right { | |
width: 420px; | |
} | |
#summary, #details { | |
height: 95%; | |
border: 1px solid blue; | |
overflow-y : auto; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">//jshint esnext:true | |
// noprotect | |
const gaussian = window.gaussian; // require('gaussian'); | |
const N45 = (mean, sd=45) => gaussian(mean, sd*sd).ppf(Math.random()); | |
const U = (min, max) => Math.floor(Math.random()*(max - min + 1) + min) | |
const removeSmallest = (arr) => { | |
arr.sort((a, b) => Math.sign(a.bet - b.bet)); | |
const numOfLosers = arr.reduce((acc, cur) => (cur.bet == arr[0].bet) ? acc+1 : acc, 0); | |
const loser = arr.splice(U(1, numOfLosers) - 1, 1); // remove one of the losers | |
return arr; | |
} | |
var table1 = jexcel(document.getElementById('details'), { | |
data:[ [ 2, 10, 'this is just a dummy line', '=B1*C1'] ], | |
columns: [ | |
{ title: 'Round', type: 'text', width:'50px', }, | |
{ title: 'inner', type: 'text', width:'50px', }, | |
{ title: 'X1', type: 'number', width:'30px', }, | |
{ title: 'X2', type: 'number', width:'30px', }, | |
{ title: 'Player-Strategy', type: 'number', width:'200px', align: 'left'}, | |
{ title: '?', type: 'number', width:'30px', }, | |
{ title: 'Bet', type: 'number', width:'30px', }, | |
], | |
rowResize: true, | |
}); | |
var dataS = [ [ 'this dummy line will be removed at the end of the program...', 10 ] ]; | |
var tableS = jexcel(document.getElementById('summary'), { | |
data:dataS, | |
columns: [ | |
{ title: 'Player-Strategy', type: 'text', width:'150px', align: 'left'}, | |
{ title: 'wins', type: 'number', width:'50px'}, | |
], | |
rowResize: true, | |
}); | |
const getPlayers = () => { | |
let arr = [ | |
{ | |
strategy: 'simple,max(x1,x2)', | |
func: (X1, X2, T) => { | |
if(X1>X2){ | |
return 'S'; | |
} else { | |
return 'R'; | |
} | |
} | |
}, | |
{ | |
strategy: 'crazy,min(x1,x2)', | |
func: (X1, X2, T) => { | |
if(X1<X2){ | |
return 'S'; | |
} else { | |
return 'R'; | |
} | |
} | |
}, | |
{ | |
strategy: 'alwaysSafe', | |
func: (X1, X2, T) => { | |
return 'S'; | |
} | |
}, | |
{ | |
strategy: 'alwaysRisk', | |
func: (X1, X2, T) => { | |
return 'R'; | |
} | |
}, | |
]; | |
// multiply each strategy | |
let mularr = []; | |
for(item in arr){ | |
for(let i=0; i<6; i++){ | |
let temp = Object.assign({}, arr[item]); | |
temp.strategy += i; | |
mularr.push(temp); | |
} | |
} | |
return mularr; | |
} | |
var winnersList = []; | |
const main = () => { | |
for(let roundNum = 0; roundNum<10000; roundNum++){ | |
let player = getPlayers(); | |
const games = player.length; | |
while(player.length>1){ | |
let X1 = U(0,150); | |
let X2 = U(0,150); | |
for(i=0; i<player.length; i++){ | |
let decision = player[i].func(X1, X2, player.length); | |
if(decision == 'S'){ | |
player[i].bet = X1; | |
}else{ | |
player[i].bet = Math.round(N45(X2)); | |
} | |
if(roundNum<2){ | |
table1.insertRow([(roundNum+1).toString(), (games - player.length + 1).toString(), X1, X2, player[i].strategy, decision, player[i].bet]); | |
} | |
} | |
player = removeSmallest(player); // find loser and remove him | |
if(player.length==1){ | |
// tableS.insertRow([(roundNum+1).toString(), player[0].strategy]); | |
winnersList.push(player[0].strategy); | |
} | |
} | |
} | |
var winnerFrequency = {}; | |
for(let i=0; i<winnersList.length; i++){ | |
if( winnerFrequency[winnersList[i]]===undefined ){ | |
winnerFrequency[winnersList[i]] = 1; | |
}else{ | |
winnerFrequency[winnersList[i]]++; | |
} | |
} | |
for(w in winnerFrequency){ | |
tableS.insertRow([w, winnerFrequency[w]]); | |
} | |
table1.deleteRow(0,1); | |
tableS.deleteRow(0,1); | |
tableS.orderBy(1); | |
tableS.orderBy(1); | |
} | |
main(); | |
</script></body> | |
</html> |
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
html, body { | |
height: 100%; | |
} | |
#left, #right { | |
height: 100%; | |
display: inline-block; | |
} | |
#left { | |
width: 200px; | |
margin-right: 60px | |
} | |
#right { | |
width: 420px; | |
} | |
#summary, #details { | |
height: 95%; | |
border: 1px solid blue; | |
overflow-y : auto; | |
} |
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
//jshint esnext:true | |
// noprotect | |
const gaussian = window.gaussian; // require('gaussian'); | |
const N45 = (mean, sd=45) => gaussian(mean, sd*sd).ppf(Math.random()); | |
const U = (min, max) => Math.floor(Math.random()*(max - min + 1) + min) | |
const removeSmallest = (arr) => { | |
arr.sort((a, b) => Math.sign(a.bet - b.bet)); | |
const numOfLosers = arr.reduce((acc, cur) => (cur.bet == arr[0].bet) ? acc+1 : acc, 0); | |
const loser = arr.splice(U(1, numOfLosers) - 1, 1); // remove one of the losers | |
return arr; | |
} | |
var table1 = jexcel(document.getElementById('details'), { | |
data:[ [ 2, 10, 'this is just a dummy line', '=B1*C1'] ], | |
columns: [ | |
{ title: 'Round', type: 'text', width:'50px', }, | |
{ title: 'inner', type: 'text', width:'50px', }, | |
{ title: 'X1', type: 'number', width:'30px', }, | |
{ title: 'X2', type: 'number', width:'30px', }, | |
{ title: 'Player-Strategy', type: 'number', width:'200px', align: 'left'}, | |
{ title: '?', type: 'number', width:'30px', }, | |
{ title: 'Bet', type: 'number', width:'30px', }, | |
], | |
rowResize: true, | |
}); | |
var dataS = [ [ 'this dummy line will be removed at the end of the program...', 10 ] ]; | |
var tableS = jexcel(document.getElementById('summary'), { | |
data:dataS, | |
columns: [ | |
{ title: 'Player-Strategy', type: 'text', width:'150px', align: 'left'}, | |
{ title: 'wins', type: 'number', width:'50px'}, | |
], | |
rowResize: true, | |
}); | |
const getPlayers = () => { | |
let arr = [ | |
{ | |
strategy: 'simple,max(x1,x2)', | |
func: (X1, X2, T) => { | |
if(X1>X2){ | |
return 'S'; | |
} else { | |
return 'R'; | |
} | |
} | |
}, | |
{ | |
strategy: 'crazy,min(x1,x2)', | |
func: (X1, X2, T) => { | |
if(X1<X2){ | |
return 'S'; | |
} else { | |
return 'R'; | |
} | |
} | |
}, | |
{ | |
strategy: 'alwaysSafe', | |
func: (X1, X2, T) => { | |
return 'S'; | |
} | |
}, | |
{ | |
strategy: 'alwaysRisk', | |
func: (X1, X2, T) => { | |
return 'R'; | |
} | |
}, | |
]; | |
// multiply each strategy | |
let mularr = []; | |
for(item in arr){ | |
for(let i=0; i<6; i++){ | |
let temp = Object.assign({}, arr[item]); | |
temp.strategy += i; | |
mularr.push(temp); | |
} | |
} | |
return mularr; | |
} | |
var winnersList = []; | |
const main = () => { | |
for(let roundNum = 0; roundNum<10000; roundNum++){ | |
let player = getPlayers(); | |
const games = player.length; | |
while(player.length>1){ | |
let X1 = U(0,150); | |
let X2 = U(0,150); | |
for(i=0; i<player.length; i++){ | |
let decision = player[i].func(X1, X2, player.length); | |
if(decision == 'S'){ | |
player[i].bet = X1; | |
}else{ | |
player[i].bet = Math.round(N45(X2)); | |
} | |
if(roundNum<2){ | |
table1.insertRow([(roundNum+1).toString(), (games - player.length + 1).toString(), X1, X2, player[i].strategy, decision, player[i].bet]); | |
} | |
} | |
player = removeSmallest(player); // find loser and remove him | |
if(player.length==1){ | |
// tableS.insertRow([(roundNum+1).toString(), player[0].strategy]); | |
winnersList.push(player[0].strategy); | |
} | |
} | |
} | |
var winnerFrequency = {}; | |
for(let i=0; i<winnersList.length; i++){ | |
if( winnerFrequency[winnersList[i]]===undefined ){ | |
winnerFrequency[winnersList[i]] = 1; | |
}else{ | |
winnerFrequency[winnersList[i]]++; | |
} | |
} | |
for(w in winnerFrequency){ | |
tableS.insertRow([w, winnerFrequency[w]]); | |
} | |
table1.deleteRow(0,1); | |
tableS.deleteRow(0,1); | |
tableS.orderBy(1); | |
tableS.orderBy(1); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment