Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

Random number geneation in c# using normal distr

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...