gohugoio/hugo · error

the maximum requested value (%v) must be a non-negative inte

Error message

the maximum requested value (%v) must be a non-negative integer <= %d

What it means

Thrown by Namespace.D when the `hi` argument (exclusive upper bound of the sampling range [0, hi)) fails cast.ToIntE, is negative, or exceeds maxSeqSize (1,000,000). hi defines the population size; it must be a non-negative integer within the package safety ceiling.

Source

Thrown at tpl/collections/collections.go:568

//
// 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
			}
			return result, nil
		}

		prng := rand.New(rand.NewPCG(key.seed, 0))

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Pass a non-negative integer literal within [0, 1000000], e.g. `D 1 3 100`.
  2. If hi is computed from a length, guard it: `{{ $hi := math.Max 0 (math.Min $len 1000000) }}`.
  3. Ensure hi is an int and not nil.
  4. If you need a larger population, reconsider the algorithm or precompute outside the template.

Example fix

// before
{{ $r := collections.D 1 3 (sub 0 5) }}
// after
{{ $r := collections.D 1 3 100 }}
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

func isValidDRange(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 3 1.5`, `collections.D 1 3 5000000`, or passing nil for hi. Guard: `cast.ToIntE(hi)` error OR hiInt < 0 OR hiInt > maxSeqSize.

Common situations: Author derives hi from a page-count that is negative (empty collection minus offset), passes a float, or intends a huge population that breaches the cap. Same shape as the n error but on the range bound.

Related errors


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