如何生成通常从一个整数范围内随机分布? [英] How to generate normally distributed random from an integer range?

查看:252
本文介绍了如何生成通常从一个整数范围内随机分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于起点和一个整数范围的结束,我怎么计算在这个范围正态分布随机整数?



我意识到正态分布进入 - +无穷大。我猜的尾巴可以截止,所以当随机被计算的范围内,重新计算之外。这种提升的整数范围中的可能性,但只要这种效果是可以容忍的(小于5%),这是细



 公共类高斯
{
私人静态布尔uselast = TRUE;
私有静态双next_gaussian = 0.0;
私有静态随机随机=新的随机();

公共静态双BoxMuller()
{
如果(uselast)
{
uselast = FALSE;
返回next_gaussian;
}
,否则
{
双V1,V2,S;

{
V1 = 2.0 * random.NextDouble() - 1.0;
V2 = 2.0 * random.NextDouble() - 1.0;
S = V1 * V1 + V2 * V2;
},而(S> = 1.0 ||小号== 0);

S = System.Math.Sqrt(( - 2.0 * System.Math.Log(S))/秒);

next_gaussian = V2 * S;
uselast = TRUE;
返回V1 * S;
}
}

公共静态双BoxMuller(双平均,双standard_deviation)
{
返回意味着+ BoxMuller()* standard_deviation;
}

公共静态INT下一页(INT分钟,INT最大值)
{
回报(INT)BoxMuller(分+(最大值 - 最小值)/ 2.0,1.0 );
}
}



我可能需要扩展的标准偏差一些如何相对。到范围,但不知道如何



答:

  //将approximitely给出最小值和最大值之间的随机高斯整数,这样的最小值和最大值是在
// 3.5与平均值(最小和最大的中途)偏差。
公共静态INT下一页(INT分钟,INT最大值)
{
双偏差= 3.5;
INT R;
,而((R =(int)的BoxMuller(分+(最大值 - 最小值)/ 2.0(最大值 - 最小值)/ 2.0 /偏差))>最大||为r分钟)
{
}

回报率 - [R;
}


解决方案

如果箱穆勒方法返回一个标准正态分布,就会产生均值为0,标准偏差1.要改造一个标准正态分布,你用X乘以你的随机数来获得标准偏差X,并添加Y以获得平均Y,如果没有记错我正确的。



查看维基百科文章的正常化标准正态分布变量部分(财产1)以更正式的证明。






在回应您的意见,经验法则是,正态分布的99.7%将是+/- 3倍标准差范围内。如果需要从0的正态分布,以100为例,比你平均会半途而废,和你的SD会(100/2)/ 3 = 16.667。所以,无论重视你得到了你箱穆勒算法,以16.667乘到拉伸的散发出来,再加入50至中心了。






约翰,为了响应你的最新评论,我真的不知道什么是下一页功能。它总是使用为1的标准差和您的最小值和最大值的中间的一个平均值。



如果你想y的平均值,以〜人数的99.7%,在范围-X到+ X,那么你只需要调用 BoxMuller(Y,X / 3)


Given the start and the end of an integer range, how do I calculate a normally distributed random integer between this range?

I realize that the normal distribution goes into -+ infinity. I guess the tails can be cutoff, so when a random gets computed outside the range, recompute. This elevates the probability of integers in the range, but as long as the this effect is tolerable (<5%), it's fine.

public class Gaussian
{
    private static bool uselast = true;
    private static double next_gaussian = 0.0;
    private static Random random = new Random();

    public static double BoxMuller()
    {
        if (uselast) 
        { 
            uselast = false;
            return next_gaussian;
        }
        else
        {
            double v1, v2, s;
            do
            {
                v1 = 2.0 * random.NextDouble() - 1.0;
                v2 = 2.0 * random.NextDouble() - 1.0;
                s = v1 * v1 + v2 * v2;
            } while (s >= 1.0 || s == 0);

            s = System.Math.Sqrt((-2.0 * System.Math.Log(s)) / s);

            next_gaussian = v2 * s;
            uselast = true;
            return v1 * s;
        }
    }

    public static double BoxMuller(double mean, double standard_deviation)
    {
        return mean + BoxMuller() * standard_deviation;
    }

    public static int Next(int min, int max)
    {
        return (int)BoxMuller(min + (max - min) / 2.0, 1.0); 
    }
}

I probably need to scale the standard deviation some how relative to the range, but don't understand how.

Answer:

    // Will approximitely give a random gaussian integer between min and max so that min and max are at
    // 3.5 deviations from the mean (half-way of min and max).
    public static int Next(int min, int max)
    {
        double deviations = 3.5;
        int r;
        while ((r = (int)BoxMuller(min + (max - min) / 2.0, (max - min) / 2.0 / deviations)) > max || r < min)
        {
        }

        return r;
    }

解决方案

If the Box-Muller method returns a "standard" normal distribution, it will have mean 0 and standard deviation 1. To transform a standard normal distribution, you multiply your random number by X to get standard deviation X, and you add Y to obtain mean Y, if memory serves me correctly.

See the Wikipedia article's section on normalizing standard normal variables (property 1) for a more formal proof.


In response to your comment, the rule of thumb is that 99.7% of a normal distribution will be within +/- 3 times the standard deviation. If you need a normal distribution from 0 to 100 for instance, than your mean will be halfway, and your SD will be (100/2)/3 = 16.667. So whatever values you get out of your Box-Muller algorithm, multiply by 16.667 to "stretch" the distribution out, then add 50 to "center" it.


John, in response to your newest comment, I'm really not sure what is the point of the Next function. It always uses a standard deviation of 1 and a mean of halfway between your min and max.

If you want a mean of Y, with ~99.7% of the numbers in the range -X to +X, then you just call BoxMuller(Y, X/3).

这篇关于如何生成通常从一个整数范围内随机分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆