连续均匀分布代码实现

public class ContinuousUniformDistribution : ContinuousDistribution
    {
        private double lowerBound;

        private double upperBound;

        public static readonly ContinuousUniformDistribution Standard = new ContinuousUniformDistribution();

        public override double Mean => (this.upperBound + this.lowerBound) / 2.0;

        public override double Variance
        {
            get
            {
                double num;
                num = this.upperBound - this.lowerBound;
                return num * num / 12.0;
            }
        }

        public override double Skewness => 0.0;

        public override double Kurtosis => -1.2;

        public override bool IsSymmetrical => true;

        public double UpperBound => this.upperBound;

        public double LowerBound => this.lowerBound;

        public static double GetRandomVariate(System.Random random, double upperBound)
        {
            if (random == null)
            {
                ThrowException.ArgumentNull("random");
            }
            return random.NextDouble() * upperBound;
        }

        public static double GetRandomVariate(System.Random random, double lowerBound, double upperBound)
        {
            if (random == null)
            {
                ThrowException.ArgumentNull("random");
            }
            return lowerBound + random.NextDouble() * (upperBound - lowerBound);
        }

        public ContinuousUniformDistribution()
            : this(0.0, 1.0)
        {
        }

        public ContinuousUniformDistribution(double upperBound)
            : this(0.0, upperBound)
        {
        }

        public ContinuousUniformDistribution(double lowerBound, double upperBound)
        {
            if (upperBound <= lowerBound)
            {
                ThrowException.ArgumentOutOfRange("upperBound");
            }
            this.lowerBound = lowerBound;
            this.upperBound = upperBound;
        }

        public override double DistributionFunction(double x)
        {
            if (x <= this.lowerBound)
            {
                return 0.0;
            }
            if (x >= this.upperBound)
            {
                return 1.0;
            }
            return (x - this.lowerBound) / (this.upperBound - this.lowerBound);
        }

        public override double ProbabilityDensityFunction(double x)
        {
            if (x < this.lowerBound || x >= this.upperBound)
            {
                return 0.0;
            }
            return 1.0 / (this.upperBound - this.lowerBound);
        }

        public override double MomentFunction(int order, double x)
        {
            if (order < 0)
            {
                ThrowException.ArgumentOutOfRange("order");
            }
            double num;
            num = this.lowerBound;
            double num2;
            num2 = this.upperBound;
            if (0.0 <= num)
            {
                if (x <= num)
                {
                    return 0.0;
                }
                num2 = Math.Min(num2, x);
            }
            else if (num2 <= 0.0)
            {
                if (x >= num2)
                {
                    return 0.0;
                }
                double num3;
                num3 = num2;
                num2 = Math.Max(num, x);
                num = num3;
            }
            else if (x >= 0.0)
            {
                num = 0.0;
                num2 = Math.Min(num2, x);
            }
            else
            {
                num2 = 0.0;
                num = Math.Max(num, x);
            }
            double num4;
            num4 = ElementaryFunctions.Pow(num, order + 1);
            return (ElementaryFunctions.Pow(num2, order + 1) - num4) / (double)(order + 1) / (this.upperBound - this.lowerBound);
        }

        public override double InverseDistributionFunction(double probability)
        {
            if (probability < 0.0 || probability > 1.0)
            {
                ThrowException.ArgumentOutOfRange("probability");
            }
            if (probability == 0.0)
            {
                return this.lowerBound;
            }
            if (probability == 1.0)
            {
                return this.upperBound;
            }
            return this.lowerBound + probability * (this.upperBound - this.lowerBound);
        }

        public override double GetRandomVariate(System.Random random)
        {
            if (random == null)
            {
                ThrowException.ArgumentNull("random");
            }
            return this.lowerBound + random.NextDouble() * (this.upperBound - this.lowerBound);
        }
    }

 如果对您有帮忙,非常感谢您支持一下创造者的付出!

 感谢支持技术分享,请扫码点赞支持:

技术合作交流qq:2401315930

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

合抱阴阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值