{"id":"184ff9f8370dfd73","repo":"drizzle-team/drizzle-orm","slug":"the-weights-for-the-weighted-random-feature-must-a","errorCode":null,"errorMessage":"The weights for the Weighted Random feature must add up to exactly 1. Please review your weights to ensure they total 1 before proceeding","messagePattern":"The weights for the Weighted Random feature must add up to exactly 1\\. Please review your weights to ensure they total 1 before proceeding","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-seed/src/services/utils.ts","lineNumber":30,"sourceCode":"\n\treturn resultList;\n};\n\nconst sumArray = (weights: number[]) => {\n\tconst scale = 1e10;\n\tconst scaledSum = weights.reduce((acc, currVal) => acc + Math.round(currVal * scale), 0);\n\treturn scaledSum / scale;\n};\n\n/**\n * @param weights positive number in range [0, 1], that represents probabilities to choose index of array. Example: weights = [0.2, 0.8]\n * @param [accuracy=100] approximate number of elements in returning array\n * @returns Example: with weights = [0.2, 0.8] and accuracy = 10 returning array of indices gonna equal this: [0, 0, 1, 1, 1, 1, 1, 1, 1, 1]\n */\nexport const getWeightedIndices = (weights: number[], accuracy = 100) => {\n\tconst weightsSum = sumArray(weights);\n\tif (weightsSum !== 1) {\n\t\tthrow new Error(\n\t\t\t`The weights for the Weighted Random feature must add up to exactly 1. Please review your weights to ensure they total 1 before proceeding`,\n\t\t);\n\t}\n\n\t// const accuracy = 100;\n\tconst weightedIndices: number[] = [];\n\tfor (const [index, weight] of weights.entries()) {\n\t\tconst ticketsNumb = Math.floor(weight * accuracy);\n\t\tweightedIndices.push(...Array.from<number>({ length: ticketsNumb }).fill(index));\n\t}\n\n\treturn weightedIndices;\n};\n\nexport const generateHashFromString = (s: string) => {\n\tlet hash = 0;\n\t// p and m are prime numbers\n\tconst p = 53;","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-seed/src/services/utils.ts#L12-L48","documentation":"The Weighted Random feature (getWeightedIndices and the WeightedRandomGenerator) requires the supplied weights to sum to exactly 1.0 so they represent a valid probability distribution. The library uses integer-scaled arithmetic (1e10 scale) to avoid floating-point drift, so the sum must be exactly 1, not approximately.","triggerScenarios":"Passing weights like [0.3, 0.3, 0.3] (sums to 0.9) or [0.5, 0.6] (sums to 1.1) to a weighted generator or to the `.with` weighted-count option.","commonSituations":"Rounding errors when splitting weights by hand; adding a new weighted option without re-normalizing the rest; copy-paste that duplicates a weight.","solutions":["Adjust the weights so they sum to exactly 1 (e.g. [0.3, 0.3, 0.4]).","Compute the last weight as 1 minus the sum of the others to guarantee the total: lastWeight = 1 - others.reduce((a,b)=>a+b,0).","Double-check for floating-point issues by rounding to a fixed number of decimals."],"exampleFix":"// before\nweights: [0.2, 0.3, 0.3]  // sums to 0.8\n// after\nweights: [0.2, 0.3, 0.5]  // sums to 1.0","handlingStrategy":"validation","validationCode":"function validateWeights(weights: number[]): string | null {\n  const scale = 1e10;\n  const sum = weights.reduce((acc, w) => acc + Math.round(w * scale), 0) / scale;\n  return sum === 1 ? null : `Weights sum to ${sum}, expected exactly 1.`;\n}\n// Auto-fix last weight:\nfunction normalizeWeights(weights: number[]): number[] {\n  const fixed = weights.slice(0, -1);\n  const last = 1 - fixed.reduce((a, b) => a + b, 0);\n  return [...fixed, Math.round(last * 1e10) / 1e10];\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always compute the last weight as 1 minus the sum of the others.","Use a schema-validation step or unit test to assert weights sum to 1.","Avoid hand-typing many small decimal weights; prefer fractions that sum cleanly."],"tags":["weighted-random","weights","validation","probability"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}