Shuffle Theory

Research into different shuffling theories and how they may effect a service as it scales across shards and clusters.

Story Engine uses what I learned here to power it’s branch narrative core.

The chance of a role being assigned to a player is determined by chance.shuffle. When a role’s chance is calculated, it can be anywhere between 70% and 130% - for now they are given as the addictive of 1 to ensure the value itself remains a positive number, to actually have a chance of being selected.

These chances are then sent through the randomizer to determine who would get the specific role on that iteration, which is repeated until all players have a role (also when no roles are left, but such a ratio allows an exact number to be distributed). Using round rather than floor or ceil factors in player distribution, but will likely become a physical blocker to advancing with role grouping / factions / teams / etc.

export function getRoles(count: number = Player.COUNT, roles: Role[] = defaultRoles): string[] {
  const sum = roles.reduce((sum, role) => sum + role.ratio, 0);
  // count / sum(roles.*.ratio)
  const scaleFactor = count / sum;

  const array = roles.flatMap<string>(role => {
    const roleCount = Math.round(role.ratio * scaleFactor);
    return Array(roleCount).fill(role.name);
  });

  return chance.shuffle(array);
}
const chances: number[] = [/* (player chances) */];
const scaleFactor = sum(chances) / chances.length;
for (const i in chances)
  chances[i] /= scaleFactor;
// ensuring all chance values are scaled to equal that of 1

This eventually turned into scale-to (NPM) (GitHub)… which failed.