gohugoio/hugo · error

the number of requested values

Error message

the number of requested values (%v) must be a non-negative integer <= %d

What it means

Thrown by Namespace.D when the `n` argument (number of requested random values) fails cast.ToIntE, is negative, or exceeds the maxSeqSize cap (1,000,000). n bounds how many unique samples are drawn from [0, hi); it must be a sane non-negative integer within the package's safety limit.

Solutions

  1. Pass a small non-negative integer literal within [0, 1000000], e.g. `D 1 5 100`.
  2. If n is computed, clamp it: `{{ $n := math.Max 0 (math.Min $computed 1000000) }}`.
  3. Ensure n is an int and not nil before the call.
  4. Audit front matter / data files feeding n for numeric validity.

Example fix

// before
{{ $r := collections.D 1 -5 10 }}
// after
{{ $r := collections.D 1 5 10 }}
Defensive patterns

Strategy: validation

Validate before calling

const maxSeqSize = 1000000
func validateDN(n any) (int, error) {
    x, err := cast.ToIntE(n)
    if err != nil || x < 0 || x > maxSeqSize {
        return 0, fmt.Errorf("n must be in [0, %d], got %v", maxSeqSize, n)
    }
    return x, nil
}

Type guard

func isValidDCount(v any) bool {
    n, err := cast.ToIntE(v)
    return err == nil && n >= 0 && n <= 1000000
}

Prevention

When it happens

Trigger: Calling `collections.D 1 -3 10`, `collections.D 1 1.5 10`, `collections.D 1 2000000 10`, or passing nil for n. The guard is `cast.ToIntE(n)` error OR nInt < 0 OR nInt > maxSeqSize.

Common situations: Author passes a computed count that overflows or is negative due to subtraction, feeds a float from a params dict, or hits the 1,000,000 ceiling with a very large hi range. Also when n comes from user-supplied front matter.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/bc319088e6d99852. Report an issue: GitHub.

Appendix: source

Thrown at tpl/collections/collections.go:563

// Method D for sequential random sampling.
//
// If n > hi, it returns the full, sorted range [0, hi) of size hi.
//
// If n == 0 or hi == 0, it returns an empty slice.
//
// Reference:
//
//	J. S. Vitter, "An efficient algorithm for sequential random sampling," ACM Trans. Math. Softw., vol. 11, no. 1, pp. 37–57, 1985.
//	See also: https://getkerf.wordpress.com/2016/03/30/the-best-algorithm-no-one-knows-about/
func (ns *Namespace) D(seed, n, hi any) ([]int, error) {
	seedInt, err := cast.ToInt64E(seed)
	if err != nil || seedInt < 0 {
		return nil, fmt.Errorf("the seed value (%v) must be a non-negative integer", seed)
	}

	nInt, err := cast.ToIntE(n)
	if err != nil || nInt < 0 || nInt > maxSeqSize {
		return nil, fmt.Errorf("the number of requested values (%v) must be a non-negative integer <= %d", n, maxSeqSize)
	}

	hiInt, err := cast.ToIntE(hi)
	if err != nil || hiInt < 0 || hiInt > maxSeqSize {
		return nil, fmt.Errorf("the maximum requested value (%v) must be a non-negative integer <= %d", hi, maxSeqSize)
	}

	if nInt == 0 || hiInt == 0 {
		return []int{}, nil
	}

	key := dKey{seed: uint64(seedInt), n: nInt, hi: hiInt}

	v, err := ns.dCache.GetOrCreate(key, func() ([]int, error) {
		if key.n > key.hi {
			result := make([]int, key.hi)
			for i := 0; i < key.hi; i++ {
				result[i] = i

View on GitHub (pinned to 52c9bd7908)