Skip to content

Instantly share code, notes, and snippets.

@progheal
Created October 8, 2023 14:56
Show Gist options
  • Save progheal/4d9c4f78ef9da2c9cc28ef6885b9d30e to your computer and use it in GitHub Desktop.
Save progheal/4d9c4f78ef9da2c9cc28ef6885b9d30e to your computer and use it in GitHub Desktop.
《西瓜遊戲得分裡的數學》(https://blog.cruciferslab.net/?p=1535) 的模擬程式
function randomFruit()
{
return Math.floor(Math.random() * 5);
}
const FruitInfo = [
{apple: 1/32, point: 3.09375}, // 櫻桃
{apple: 1/16, point: 5.1875}, // 草莓
{apple: 1/8, point: 7.375}, // 葡萄
{apple: 1/4, point: 8.75}, // 柑橘
{apple: 1/2, point: 7.5}, // 柿子
];
function applePoints(draws)
{
let appleEquiv = 0, extraPoint = 0;
for(let i = 0; i < draws; i++)
{
let fruit = randomFruit();
appleEquiv += FruitInfo[fruit].apple;
extraPoint += FruitInfo[fruit].point;
}
return extraPoint / appleEquiv;
}
const RUNS = 10_000_000;
let sum = 0, sqsum = 0;
for(let i = 0; i < RUNS; i++)
{
let result = applePoints(250);
sum += result;
sqsum += result * result;
if((i+1) % 100_000 == 0)
{
let avg = sum / (i+1);
let sqavg = sqsum / (i+1);
let stddev = (sqavg - avg ** 2) ** 0.5;
console.log((i+1), //局數
avg.toFixed(4), //平均蘋果值
stddev.toFixed(4), //蘋果值標準差
(avg * 32 + 849).toFixed(4), //換算成的平均西瓜值
(stddev * 2 * 32).toFixed(4)); //兩個標準差 (95%) 對應的西瓜值差距
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment