Random number geneation in c# using normal distr

房东的猫 提交于 2019-12-13 07:58:48

问题


i am new to c# and i am trying to generate numbers ifrom normal distribution in c#. I serched the web and i found only some code. I would like to use a ready built in function and not a code!! any suggestions?


回答1:


You will still need to do a little coding too:

  1. Define your normal distribution.
  2. Sample from it.

N.B You need to define it once and then sample as opposed to re-define.

Maybe this little class can help, then you can just use it in your code where you need...

    public class BoxMullerNormal
        {                       
            private MathNet.Numerics.Distributions.Normal normal;

            public BoxMullerNormal(double mean = 0,double std = .01)
            {
                normal = new MathNet.Numerics.Distributions.Normal(mean,std);            
            }

            public override dynamic getRandom()
            {
                // Implementation Uses C#MathNet.Numerics Normal Distribution Sampling
                return normal.Sample();                          
            }
}

Initialize the class at the start of your app to define the normal, then just call getRandom() every time to sample from it. You can also add the class to once of your existing Interfaces.




回答2:


The MathNet Numerics library has a number of different random number generators. I'm pretty sure normal distributions are one of the options.

Read this

If you scroll to the bottom, I believe it will have what you are looking for.



来源:https://stackoverflow.com/questions/27078101/random-number-geneation-in-c-sharp-using-normal-distr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!