{"id":"da72a7c95b30cab7","repo":"gohugoio/hugo","slug":"the-seed-value-v-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"the seed value (%v) must be a non-negative integer","messagePattern":"the seed value \\((.+?)\\) must be a non-negative integer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/collections.go","lineNumber":558,"sourceCode":"// D returns a sorted slice of unique random integers in the half-open interval\n// [0, hi) using the provided seed value. The number of elements in the\n// resulting slice is n or hi, whichever is less.\n//\n// If n <= hi, it returns a sorted random sample of size n using J. S. Vitter’s\n// 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","sourceCodeStart":540,"sourceCodeEnd":576,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/collections/collections.go#L540-L576","documentation":"`collections.D` generates a deterministic sorted random sample using a PCG PRNG seeded from the first argument. The seed must cast to a non-negative int64; negative values or values that fail `cast.ToInt64E` (non-numeric strings, floats with bad format, nil) produce this error.","triggerScenarios":"`{{ collections.D -1 5 100 }}`, `{{ collections.D \"abc\" 5 100 }}`, or a nil/unset seed variable passed as the first argument.","commonSituations":"Deriving the seed from a hash or timestamp expression that yields a negative number; passing a page title or other string as seed expecting automatic hashing; unset param defaulting to nil.","solutions":["Pass a non-negative integer seed, e.g. `{{ collections.D 42 5 100 }}`.","To derive a seed from a string, hash it to a non-negative int first (e.g. `crypto.FNV32a`).","Default unset params: `{{ $seed := default 0 .Params.seed }}`."],"exampleFix":"<!-- before -->\n{{ collections.D .Title 5 100 }}\n<!-- after -->\n{{ collections.D (crypto.FNV32a .Title) 5 100 }}","handlingStrategy":"validation","validationCode":"{{ $seed := int (default 0 .Params.seed) }}\n{{ if lt $seed 0 }}{{ errorf \"seed must be >= 0, got %v\" $seed }}{{ end }}\n{{ $sample := collections.Shuffle ... }}","typeGuard":"{{ $validSeed := and (eq (printf \"%T\" $seed) \"int\") (ge $seed 0) }}","tryCatchPattern":null,"preventionTips":["Coerce seed values from front matter/params with `int` and clamp negatives (e.g. `math.Max 0 $seed`).","Avoid deriving seeds by subtraction that can go negative.","Use a fixed literal seed for reproducible builds instead of computed values."],"tags":["hugo","templates","random","validation"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}