{"record":{"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/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/collections.go#L540-L576","documentation":"Thrown by Namespace.D — Hugo's sorted-unique-random-integer generator (Vitter's Method D) — when the `seed` argument fails to convert to int64 or is negative. The PRNG is seeded with this value, so it must be a deterministic non-negative integer; negatives and non-numeric values are rejected outright at validation time before any sampling occurs.","triggerScenarios":"Calling `{{ partial \"D\" (dict \"seed\" -5 \"n\" 3 \"hi\" 10) }}` or the namespaced `collections.D` with a negative seed, a float like `1.5`, a string that cast cannot parse, or nil. The check is `cast.ToInt64E(seed)` returning an error OR seedInt < 0.","commonSituations":"Content author computes a seed from a date/hash that can be negative, passes a template variable that is unexpectedly nil or a string, or copies example code that hard-coded a seed which was later changed. Also seen when seeding from `math/rand` output cast improperly.","solutions":["Pass an explicit non-negative integer literal, e.g. `D 42 3 10`.","If the seed is computed, clamp it: `{{ $seed := mod $hash 1000000 }}` to guarantee non-negativity.","Verify the seed variable is an int and not nil before calling D.","Check upstream data sources (front matter, JSON) that feed the seed are numeric."],"exampleFix":"// before\n{{ $r := collections.D -1 3 10 }}\n// after\n{{ $r := collections.D 42 3 10 }}","handlingStrategy":"validation","validationCode":"func validateDSeed(seed any) (int64, error) {\n    s, err := cast.ToInt64E(seed)\n    if err != nil || s < 0 {\n        return 0, fmt.Errorf(\"seed must be non-negative int, got %v\", seed)\n    }\n    return s, nil\n}","typeGuard":"func isNonNegInt(v any) bool {\n    n, err := cast.ToInt64E(v)\n    return err == nil && n >= 0\n}","tryCatchPattern":null,"preventionTips":["Pass an explicit non-negative integer seed literal.","Clamp computed seeds modulo a positive modulus before calling D.","Verify seed is non-nil and numeric in partial preconditions.","Avoid sourcing seeds from operations that can yield negatives (e.g. subtraction)."],"tags":["go","hugo","template","collections","random","validation"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}