{"id":"bc319088e6d99852","repo":"gohugoio/hugo","slug":"the-number-of-requested-values-v-must-be-a-non","errorCode":null,"errorMessage":"the number of requested values (%v) must be a non-negative integer <= %d","messagePattern":"the number of requested values \\((.+?)\\) must be a non-negative integer <= (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/collections.go","lineNumber":563,"sourceCode":"// Method D for sequential random sampling.\n//\n// If n > hi, it returns the full, sorted range [0, hi) of size hi.\n//\n// If n == 0 or hi == 0, it returns an empty slice.\n//\n// Reference:\n//\n//\tJ. S. Vitter, \"An efficient algorithm for sequential random sampling,\" ACM Trans. Math. Softw., vol. 11, no. 1, pp. 37–57, 1985.\n//\tSee also: https://getkerf.wordpress.com/2016/03/30/the-best-algorithm-no-one-knows-about/\nfunc (ns *Namespace) D(seed, n, hi any) ([]int, error) {\n\tseedInt, err := cast.ToInt64E(seed)\n\tif err != nil || seedInt < 0 {\n\t\treturn nil, fmt.Errorf(\"the seed value (%v) must be a non-negative integer\", seed)\n\t}\n\n\tnInt, err := cast.ToIntE(n)\n\tif err != nil || nInt < 0 || nInt > maxSeqSize {\n\t\treturn nil, fmt.Errorf(\"the number of requested values (%v) must be a non-negative integer <= %d\", n, maxSeqSize)\n\t}\n\n\thiInt, err := cast.ToIntE(hi)\n\tif err != nil || hiInt < 0 || hiInt > maxSeqSize {\n\t\treturn nil, fmt.Errorf(\"the maximum requested value (%v) must be a non-negative integer <= %d\", hi, maxSeqSize)\n\t}\n\n\tif nInt == 0 || hiInt == 0 {\n\t\treturn []int{}, nil\n\t}\n\n\tkey := dKey{seed: uint64(seedInt), n: nInt, hi: hiInt}\n\n\tv, err := ns.dCache.GetOrCreate(key, func() ([]int, error) {\n\t\tif key.n > key.hi {\n\t\t\tresult := make([]int, key.hi)\n\t\t\tfor i := 0; i < key.hi; i++ {\n\t\t\t\tresult[i] = i","sourceCodeStart":545,"sourceCodeEnd":581,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/collections/collections.go#L545-L581","documentation":"The second argument to `collections.D` is `n`, the number of random values requested. It must cast to an int in [0, 1,000,000] (`maxSeqSize`); negative values, values over the cap, or non-numeric inputs fail this check, preventing oversized allocations.","triggerScenarios":"`{{ collections.D 42 -1 100 }}`, `{{ collections.D 42 2000000 100 }}`, or a non-numeric `n` such as a string param.","commonSituations":"Computing `n` from `sub` expressions that go negative when a collection is empty; using a large byte count or ID as the sample size by mistake.","solutions":["Pass `n` in range 0–1,000,000: `{{ collections.D 42 5 100 }}`.","Clamp computed values: `{{ $n := math.Max 0 (sub (len $items) 1) }}`.","Note n > hi is fine (returns the full range) — only negatives and > 1,000,000 fail."],"exampleFix":"<!-- before -->\n{{ collections.D 42 (sub (len $s) 10) 100 }}\n<!-- after -->\n{{ collections.D 42 (math.Max 0 (sub (len $s) 10)) 100 }}","handlingStrategy":"validation","validationCode":"{{ $want := int .Params.count }}\n{{ $max := len $items }}\n{{ if or (lt $want 0) (gt $want $max) }}\n  {{ $want = math.Min (math.Max 0 $want) $max }}\n{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp the requested count to [0, len(collection)] before sampling.","Validate user-configurable counts (site params, front matter) with `int` coercion and bounds checks.","Use `first` when you just need the leading N items — it tolerates N > len."],"tags":["hugo","templates","random","limits","validation"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}