/// <summary> /// Returns a random double value according to the specified configuration. /// </summary> /// <param name="randomValueCfg">The random value configuration.</param> /// <param name="rand"></param> public static double NextDouble(this Random rand, RandomValueSettings randomValueCfg) { double value; switch (randomValueCfg.DistrType) { case RandomCommon.DistributionType.Uniform: value = rand.NextRangedUniformDouble(randomValueCfg.Min, randomValueCfg.Max); break; case RandomCommon.DistributionType.Gaussian: if (randomValueCfg.DistrCfg != null) { GaussianDistrSettings gaussianCfg = randomValueCfg.DistrCfg as GaussianDistrSettings; value = rand.NextRangedGaussianDouble(gaussianCfg.Mean, gaussianCfg.StdDev, randomValueCfg.Min, randomValueCfg.Max); } else { throw new ArgumentException($"A specific configuration of the Gaussian distribution is missing.", "randomValueCfg"); } break; case RandomCommon.DistributionType.Exponential: if (randomValueCfg.DistrCfg != null) { ExponentialDistrSettings exponentialCfg = randomValueCfg.DistrCfg as ExponentialDistrSettings; value = rand.NextRangedExponentialDouble(exponentialCfg.Mean, randomValueCfg.Min, randomValueCfg.Max); } else { throw new ArgumentException($"A specific configuration of the Exponential distribution is missing.", "randomValueCfg"); } break; case RandomCommon.DistributionType.Gamma: if (randomValueCfg.DistrCfg != null) { GammaDistrSettings gammaCfg = randomValueCfg.DistrCfg as GammaDistrSettings; value = rand.NextRangedGammaDouble(gammaCfg.Alpha, gammaCfg.Beta, randomValueCfg.Min, randomValueCfg.Max); } else { throw new ArgumentException($"A specific configuration of the Gamma distribution is missing.", "randomValueCfg"); } break; default: throw new ArgumentException($"Unknown distribution type: {randomValueCfg.DistrType}.", "randomValueCfg"); } if (randomValueCfg.RandomSign) { value *= rand.NextSign(); } return(value); }
/// <summary> /// Returns random double according to specified settings. /// </summary> /// <param name="rand"></param> /// <param name="settings">Encapsulated settings</param> public static double NextDouble(this Random rand, RandomValueSettings settings) { double value; switch (settings.DistrType) { case RandomCommon.DistributionType.Uniform: value = rand.NextRangedUniformDouble(settings.Min, settings.Max); break; case RandomCommon.DistributionType.Gaussian: if (settings.DistrCfg != null) { GaussianDistrSettings gaussianCfg = settings.DistrCfg as GaussianDistrSettings; value = rand.NextFilterredGaussianDouble(gaussianCfg.Mean, gaussianCfg.StdDev, settings.Min, settings.Max); } else { throw new InvalidOperationException($"Configuration of Gaussian distribution is missing"); } break; case RandomCommon.DistributionType.Exponential: if (settings.DistrCfg != null) { ExponentialDistrSettings exponentialCfg = settings.DistrCfg as ExponentialDistrSettings; value = rand.NextFilterredExponentialDouble(exponentialCfg.Mean, settings.Min, settings.Max); } else { throw new InvalidOperationException($"Configuration of Exponential distribution is missing"); } break; case RandomCommon.DistributionType.Gamma: if (settings.DistrCfg != null) { GammaDistrSettings gammaCfg = settings.DistrCfg as GammaDistrSettings; value = rand.NextFilterredGammaDouble(gammaCfg.Alpha, gammaCfg.Beta, settings.Min, settings.Max); } else { throw new InvalidOperationException($"Configuration of Gamma distribution is missing"); } break; default: throw new InvalidOperationException($"Unknown distribution type {settings.DistrType}."); } if (settings.RandomSign) { value *= rand.NextSign(); } return(value); }
/// <summary> /// Returns a random double value. /// </summary> /// <remarks> /// Follows the Gaussian distribution. /// </remarks> /// <param name="distrCfg">Configuration of the Gaussian distribution.</param> /// <param name="rand"></param> public static double NextGaussianDouble(this Random rand, GaussianDistrSettings distrCfg) { return(NextGaussianDouble(rand, distrCfg.Mean, distrCfg.StdDev)); }