random-tables-mcp
Version:
An MCP (Model Context Protocol) server for managing and rolling on random-table assets used in tabletop RPGs. Create, update, and roll on random tables with support for nested tables, weighted entries, and range-based results.
40 lines • 1.54 kB
JavaScript
/**
* Default implementation of the RandomNumberGenerator interface.
* Uses JavaScript's Math.random() for generating random numbers.
*/
export class DefaultRandomNumberGenerator {
/**
* Generates a random number between min and max (inclusive).
* @param min The minimum value (inclusive).
* @param max The maximum value (inclusive).
* @returns A random number between min and max.
*/
getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Gets a weighted random index based on the provided weights.
* @param weights An array of weights.
* @returns A random index, with probability proportional to the weights.
* @throws Error if the weights array is empty or contains non-positive values.
*/
getWeightedIndex(weights) {
if (weights.length === 0) {
throw new Error('Weights array cannot be empty');
}
if (weights.some(weight => weight <= 0)) {
throw new Error('All weights must be positive');
}
const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
let randomValue = Math.random() * totalWeight;
for (let i = 0; i < weights.length; i++) {
randomValue -= weights[i];
if (randomValue <= 0) {
return i;
}
}
// Fallback (should never happen with proper weights)
return weights.length - 1;
}
}
//# sourceMappingURL=default-random-number-generator.js.map