{"record":{"id":"b3e49898b6a4c74f","repo":"TheAlgorithms/Go","slug":"x-must-be-n-given-values-are-x-d-n-d","errorCode":null,"errorMessage":"x must be < n - given values are x=%d, n=%d","messagePattern":"x must be < n - given values are x=(.+?), n=(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/pi/montecarlopi.go","lineNumber":78,"sourceCode":"// the number of points which where within the circle of center 0 and radius 1 (unit circle)\nfunc drawPoints(n int, c chan<- int) {\n\trnd := rand.New(rand.NewSource(time.Now().UnixNano()))\n\tinside := 0\n\tfor i := 0; i < n; i++ {\n\t\tx, y := rnd.Float64(), rnd.Float64()\n\t\tif x*x+y*y <= 1 {\n\t\t\tinside++\n\t\t}\n\t}\n\tc <- inside\n}\n\n// splitInt takes an integer x and splits it within an integer slice of length n in the most uniform\n// way possible.\n// For example, splitInt(10, 3) will return []int{4, 3, 3}, nil\nfunc splitInt(x int, n int) ([]int, error) {\n\tif x < n {\n\t\treturn nil, fmt.Errorf(\"x must be < n - given values are x=%d, n=%d\", x, n)\n\t}\n\tsplit := make([]int, n)\n\tif x%n == 0 {\n\t\tfor i := 0; i < n; i++ {\n\t\t\tsplit[i] = x / n\n\t\t}\n\t} else {\n\t\tlimit := x % n\n\t\tfor i := 0; i < limit; i++ {\n\t\t\tsplit[i] = x/n + 1\n\t\t}\n\t\tfor i := limit; i < n; i++ {\n\t\t\tsplit[i] = x / n\n\t\t}\n\t}\n\treturn split, nil\n}\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/pi/montecarlopi.go#L60-L96","documentation":"splitInt distributes x items uniformly across n workers/channels; it requires x >= n so every slot gets at least one item (MonteCarloPiConcurrent passes total iterations vs worker count), and reports both values when the precondition fails.","triggerScenarios":"Thrown at math/pi/montecarlopi.go:78 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the number of iterations is at least the number of workers before calling","Clamp n to min(n, x) at the call site","Skip the split entirely when x < n and handle x items in one batch"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}