Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: "averageTotal" for "Keep" modifiers calculates incorrectly #293

Open
JPStrydom opened this issue Jan 2, 2024 · 0 comments
Open
Assignees
Labels

Comments

@JPStrydom
Copy link

Description

Firstly, thanks for the awesome tool! Really easy to use and super useful.

I'm not sure about he maths happening behind the scenes for the averageTotal calculations, but I've noticed that the values it returns when the Keep modifier is used looks incorrect.

I ran the following to test the average:

const { DiceRoll } = require('@dice-roller/rpg-dice-roller');

const getAverage = numbers => numbers.reduce((total, number) => total + number) / numbers.length;

const getSuccessRate = (numbers, target) =>
  getAverage(numbers.map(number => number >= target)) * 100;

const executeRolls = (rollTypes, { target = 10, numberOfRolls = 1000 } = {}) => {
  const rollResults = rollTypes.map(rollType => ({ rollType, rolls: [] }));

  for (let i = 0; i < numberOfRolls; i++) {
    rollResults.forEach(rollResult =>
      rollResult.rolls.push(new DiceRoll(rollResult.rollType).total)
    );
  }

  return rollResults.map(({ rollType, rolls }) => ({
    rollType,
    average: getAverage(rolls),
    rpgDiceRollerAverage: new DiceRoll(rollType).averageTotal,
    successRate: getSuccessRate(rolls, target)
  }));
};

console.log(
  executeRolls(['1d20', '2d20', '1d20 + 1d4', '2d20 + 2d4', '2d20k1', '3d20k1'], {
    numberOfRolls: 100000,
    target: 15
  })
);

Which outputs the following:

[
  {
    rollType: '1d20',
    average: 10.49794,
    rpgDiceRollerAverage: 10.5,
    successRate: 30.076000000000004
  },
  {
    rollType: '2d20',
    average: 21.00188,
    rpgDiceRollerAverage: 21,
    successRate: 77.286
  },
  {
    rollType: '1d20 + 1d4',
    average: 13.04473,
    rpgDiceRollerAverage: 13,
    successRate: 42.844
  },
  {
    rollType: '2d20 + 2d4',
    average: 26.01443,
    rpgDiceRollerAverage: 26,
    successRate: 90.688
  },
  {
    rollType: '2d20k1',
    average: 13.81472,
    rpgDiceRollerAverage: 10.5,
    successRate: 50.775000000000006
  },
  {
    rollType: '3d20k1',
    average: 15.47827,
    rpgDiceRollerAverage: 10.5,
    successRate: 65.549
  }
]

As you can see, all the average calculations that the rpg-dice-roller made were correct, except for those using a Keep modifier.

Here's a summary of the Keep modifier's expected and actual average values:

Roll Type RGB Dice Roller Average True Average
'2d20k1' 10.50 ~13.81472
'3d20k1' 10.50 ~15.47827

Dice Roller version

5.5.0

Bundle type

UMD

Node version

20.10.0

Browser name and version

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants