{"record":{"id":"e103fb0fdd9a8e5d","repo":"PowerShell/PowerShell","slug":"minvalue-cannot-be-greater-than-maxvalue","errorCode":null,"errorMessage":"'minValue' cannot be greater than maxValue.","messagePattern":"'minValue' cannot be greater than maxValue\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommandBase.cs","lineNumber":649,"sourceCode":"            if (maxValue < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(maxValue), GetRandomCommandStrings.MaxMustBeGreaterThanZeroApi);\n            }\n\n            return Next(0, maxValue);\n        }\n\n        /// <summary>\n        /// Returns a random integer that is within a specified range.\n        /// </summary>\n        /// <param name=\"minValue\">The inclusive lower bound of the random number returned.</param>\n        /// <param name=\"maxValue\">The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>\n        /// <returns>Next random integer.</returns>\n        public int Next(int minValue, int maxValue)\n        {\n            if (minValue > maxValue)\n            {\n                throw new ArgumentOutOfRangeException(nameof(minValue), GetRandomCommandStrings.MinGreaterThanOrEqualMaxApi);\n            }\n\n            int randomNumber = 0;\n\n            long range = (long)maxValue - (long)minValue;\n            if (range <= int.MaxValue)\n            {\n                randomNumber = (int)(NextDouble() * range) + minValue;\n            }\n            else\n            {\n                double largeSample = InternalSampleLargeRange() * (1.0 / (2 * ((uint)int.MaxValue)));\n                randomNumber = (int)((long)(largeSample * range) + minValue);\n            }\n\n            return randomNumber;\n        }\n","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/PowerShell/PowerShell/blob/3ff3c711bf54a18f8440f3c5190c3ac91cdc5852/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetRandomCommandBase.cs#L631-L667","documentation":"Thrown by the Get-Random cmdlet's internal generator method Next(int minValue, int maxValue) when the inclusive lower bound exceeds the exclusive upper bound. The contract is maxValue >= minValue; the method guards this before computing the random range and refuses to produce a nonsensical result.","triggerScenarios":"Calling Get-Random -Minimum N -Maximum M where N > M, or invoking the public Next(minValue,maxValue) API directly with minValue greater than maxValue (e.g. Next(10, 5)).","commonSituations":"Swapped -Minimum/-Maximum argument order, computing bounds from data where the 'max' value is unexpectedly smaller than the 'min' (empty range, off-by-one), or passing user-supplied range values without ordering them.","solutions":["Ensure -Maximum is greater than -Minimum before calling Get-Random; swap if the caller's inputs are unordered","Compute bounds defensively: int lo=Math.Min(a,b), hi=Math.Max(a,b) then use lo as min, hi as max","Validate the range up front and surface a clear caller-side error rather than letting Get-Random throw"],"exampleFix":"// before\nGet-Random -Minimum 100 -Maximum 10\n\n// after\nGet-Random -Minimum 10 -Maximum 100","handlingStrategy":"validation","validationCode":"if ($Minimum -gt $Maximum) { throw \"Minimum ($Minimum) must be <= Maximum ($Maximum)\" }\nGet-Random -Minimum $Minimum -Maximum $Maximum","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Order caller-supplied bounds with [Math]::Min/[Math]::Max before passing them to Get-Random","Validate ranges at the boundary of your script/function before they reach Get-Random"],"tags":["random","argument-validation","get-random"],"backgroundTag":null,"analyzedSha":"3ff3c711bf54a18f8440f3c5190c3ac91cdc5852","analyzedAt":"2026-08-13T10:39:10.759Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}