drizzle-team/drizzle-orm · error · Error

The weights for the Weighted Random feature must add up to e

Error message

The weights for the Weighted Random feature must add up to exactly 1. Please review your weights to ensure they total 1 before proceeding

What it means

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.

Source

Thrown at drizzle-seed/src/services/utils.ts:30

	return resultList;
};

const sumArray = (weights: number[]) => {
	const scale = 1e10;
	const scaledSum = weights.reduce((acc, currVal) => acc + Math.round(currVal * scale), 0);
	return scaledSum / scale;
};

/**
 * @param weights positive number in range [0, 1], that represents probabilities to choose index of array. Example: weights = [0.2, 0.8]
 * @param [accuracy=100] approximate number of elements in returning array
 * @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]
 */
export const getWeightedIndices = (weights: number[], accuracy = 100) => {
	const weightsSum = sumArray(weights);
	if (weightsSum !== 1) {
		throw new Error(
			`The weights for the Weighted Random feature must add up to exactly 1. Please review your weights to ensure they total 1 before proceeding`,
		);
	}

	// const accuracy = 100;
	const weightedIndices: number[] = [];
	for (const [index, weight] of weights.entries()) {
		const ticketsNumb = Math.floor(weight * accuracy);
		weightedIndices.push(...Array.from<number>({ length: ticketsNumb }).fill(index));
	}

	return weightedIndices;
};

export const generateHashFromString = (s: string) => {
	let hash = 0;
	// p and m are prime numbers
	const p = 53;

View on GitHub (pinned to b7862528fd)

Solutions

  1. Adjust the weights so they sum to exactly 1 (e.g. [0.3, 0.3, 0.4]).
  2. 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).
  3. Double-check for floating-point issues by rounding to a fixed number of decimals.

Example fix

// before
weights: [0.2, 0.3, 0.3]  // sums to 0.8
// after
weights: [0.2, 0.3, 0.5]  // sums to 1.0
Defensive patterns

Strategy: validation

Validate before calling

function validateWeights(weights: number[]): string | null {
  const scale = 1e10;
  const sum = weights.reduce((acc, w) => acc + Math.round(w * scale), 0) / scale;
  return sum === 1 ? null : `Weights sum to ${sum}, expected exactly 1.`;
}
// Auto-fix last weight:
function normalizeWeights(weights: number[]): number[] {
  const fixed = weights.slice(0, -1);
  const last = 1 - fixed.reduce((a, b) => a + b, 0);
  return [...fixed, Math.round(last * 1e10) / 1e10];
}

Prevention

When it happens

Trigger: 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.

Common situations: Rounding errors when splitting weights by hand; adding a new weighted option without re-normalizing the rest; copy-paste that duplicates a weight.

Related errors


AI-assisted analysis of drizzle-team/drizzle-orm@b7862528fd (2026-08-03). Data as JSON: /data/errors/184ff9f8370dfd73.json. Report an issue: GitHub.