# D&D Bonus XP Analysis

a guest
Jan 3rd, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. let experimentCount = 10000;
2. console.log(`Performing \${experimentCount} experiments using two fighters until one of them reaches level 12.`);
3. repeatExperiments(experimentCount, 5, 0.2);
4. repeatExperiments(experimentCount, 10, 0.2);
5. repeatExperiments(experimentCount, 20, 0.2);
6.
7. repeatExperiments(experimentCount, 5, 0.4);
8. repeatExperiments(experimentCount, 10, 0.4);
9. repeatExperiments(experimentCount, 20, 0.4);
10.
11. repeatExperiments(experimentCount, 5, 0.8);
12. repeatExperiments(experimentCount, 10, 0.8);
13. repeatExperiments(experimentCount, 20, 0.8);
14.
15. repeatExperiments(experimentCount, 5, 1.6);
16. repeatExperiments(experimentCount, 10, 1.6);
17. repeatExperiments(experimentCount, 20, 1.6);
18.
19. function repeatExperiments(experimentCount, bonusXp, xpGainFactor) {
20.     let sumMattered = 0;
21.     let sumSessions = 0;
22.
23.     for(let i = 0; i < experimentCount; i++) {
24.         let results = performExperiment(bonusXp, xpGainFactor);
25.         sumMattered += results.mattered;
26.         sumSessions += results.sessions;
27.     }
28.
29.     let ratio = Math.round(1000 * sumMattered / sumSessions) / 10;
30.     let averageSessions = Math.round(sumSessions / experimentCount);
31.     let averageMattered = Math.round(ratio * 0.01 * averageSessions);
32.
33.     console.log(`\${bonusXp}% Bonus XP was on average relevant to \${averageMattered} sessions (\${ratio}%), \${averageSessions} sessions to hit level 12.`);
34. }
35.
36. function performExperiment(bonusXp, xpGainFactor) {
37.     let bonusXpPercent = (bonusXp * 0.01) + 1;
38.     let fighterXp = [200,400,800,1500,3000,6500,12000,20000,30000,40000,60000];
39.
40.     let fighter1 = { xp: 0, level: 1 };
41.     let fighter2 = { xp: 0, level: 1 };
42.     let sessions = 0;
43.     let bonusXpMattered = 0;
44.
45.     for(let i = 0; i < 200; i++) {
46.         sessions++;
47.         let threshold = fighter1.xp < 20000 ? fighter1.xp : 20000;
48.         threshold = threshold < 200 ? 200 : threshold;
49.
50.         let xpGain = threshold * xpGainFactor * Math.random();
51.
52.         fighter1.xp += Math.round(xpGain);
53.         fighter2.xp += Math.round(xpGain * bonusXpPercent);
54.
55.         fighter1.level = getFighterLevel(fighter1.xp);
56.         fighter2.level = getFighterLevel(fighter2.xp);
57.
58.         //console.log(`Session \${sessions}. Fighter \${fighter1.level} (\${fighter1.xp}), Fighter \${fighter2.level} (\${fighter2.xp})`);
59.
60.         if (fighter1.level != fighter2.level) {
61.             bonusXpMattered++;
62.         }
63.
64.         if (fighter1.level == 12) break;
65.     }
66.
67.     function getFighterLevel(xp) {
68.         let level = fighterXp.findIndex(x => x > xp);
69.         if (level == -1) level = 11;
70.         return level + 1;
71.     }
72.
73.     return { sessions, mattered: bonusXpMattered };
74. }