{"record":{"id":"06f6365d34e6e326","repo":"StockSharp/StockSharp","slug":"random-count-must-be-positive","errorCode":null,"errorMessage":"Random count must be positive.","messagePattern":"Random count must be positive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Algo.Strategies/StrategyParamHelper.cs","lineNumber":674,"sourceCode":"\t/// <returns>Lazy enumerable of strategy clones with their parameter arrays.</returns>\n\tpublic static IEnumerable<(Strategy strategy, IStrategyParam[] parameters)> ToBruteForceRandom(\n\t\tthis Strategy strategy,\n\t\tIStrategyParam[] parameters,\n\t\tint randomCount,\n\t\tout IStrategyParam[] optimizedParams,\n\t\tout int totalCount)\n\t{\n\t\tif (strategy is null)\n\t\t\tthrow new ArgumentNullException(nameof(strategy));\n\n\t\tif (parameters is null)\n\t\t\tthrow new ArgumentNullException(nameof(parameters));\n\n\t\tif (parameters.Length == 0)\n\t\t\tthrow new ArgumentOutOfRangeException(nameof(parameters), \"No optimization parameters.\");\n\n\t\tif (randomCount <= 0)\n\t\t\tthrow new ArgumentOutOfRangeException(nameof(randomCount), \"Random count must be positive.\");\n\n\t\tvar singleParams = new List<IStrategyParam>();\n\t\tvar optimizeDict = new Dictionary<string, (IStrategyParam param, HashSet<object> values)>();\n\n\t\tforeach (var param in parameters)\n\t\t{\n\t\t\tif (!param.CanOptimize || param.OptimizeFrom is null || param.OptimizeTo is null)\n\t\t\t{\n\t\t\t\tsingleParams.Add(param);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tvar values = new HashSet<object>();\n\n\t\t\tfor (var i = 0; i < randomCount; i++)\n\t\t\t{\n\t\t\t\tvar randomValue = param.GetRandom(strategy.RandomProvider);\n\t\t\t\tif (randomValue is not null)","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/StockSharp/StockSharp/blob/601a191de678bff83da28b14828f8885214ca71c/Algo.Strategies/StrategyParamHelper.cs#L656-L692","documentation":"Thrown by ToBruteForceRandom when randomCount <= 0. randomCount is the number of random samples drawn per optimizable parameter; a non-positive count produces no samples, so it is rejected with ArgumentOutOfRangeException.","triggerScenarios":"Calling strategy.ToBruteForceRandom(parameters, randomCount, ...) with randomCount set to 0 or a negative value — e.g. a UI/config value that defaulted to 0 or was parsed from an empty input.","commonSituations":"User-facing 'sample size' field left blank or set to 0; randomCount derived from a calculation (e.g. budget / cost) that rounded down to 0; passing an uninitialized int field.","solutions":["Pass a positive integer for randomCount (e.g. at least 1; typically tens or hundreds for meaningful coverage).","Clamp/validate the source value before calling: randomCount = Math.Max(1, configuredCount).","Bind the UI input to a numeric up-down with a minimum of 1."],"exampleFix":"// before\nvar runs = strategy.ToBruteForceRandom(opt, configuredSamples, out _, out _); // configuredSamples == 0 -> throws\n\n// after\nvar samples = Math.Max(1, configuredSamples);\nvar runs = strategy.ToBruteForceRandom(opt, samples, out _, out _);","handlingStrategy":"validation","validationCode":"var samples = Math.Max(1, configuredSampleCount);\nstrategy.ToBruteForceRandom(opt, samples, out _, out _);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bind sample-size inputs to a numeric control with minimum 1.","Clamp computed sample counts to at least 1 before passing."],"tags":["optimization","random-search","argument-validation","csharp"],"backgroundTag":null,"analyzedSha":"601a191de678bff83da28b14828f8885214ca71c","analyzedAt":"2026-08-13T20:43:24.460Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}