ecclesia
Version:
Framework for political and electoral simulations
22 lines • 936 B
JavaScript
import { NumberCounter } from "@gouvernathor/python/collections";
import { createRandomObj } from "../../utils";
/**
* Randomized attribution.
* Everyone votes, then one ballot is selected at random.
* (One ballot per seat to fill, and assuming there's
* enough ballots that the picking is with replacement.)
*
* The randomization is based on the given parameters. If a RNG object
* is passed, it is used without reseeding across all calls of the attribution.
* If a seed is passed, the random object is reseeded
* at each call of the attribution.
*/
export function randomize({ nSeats, ...randomParam }) {
const attrib = (votes, _rest = {}) => {
const randomObj = createRandomObj(randomParam);
return NumberCounter.fromKeys(randomObj.choices([...votes.keys()], { weights: [...votes.values()], k: nSeats }));
};
attrib.nSeats = nSeats;
return attrib;
}
//# sourceMappingURL=randomFactory.js.map