c#中使用普通dstr的随机数生成 [英] Random number geneation in c# using normal distr

查看:56
本文介绍了c#中使用普通dstr的随机数生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 c# 新手,我正在尝试从 c# 中的正态分布生成数字.我搜索了网络,我只找到了一些代码.我想使用现成的内置函数而不是代码!!有什么建议吗?

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?

推荐答案

你仍然需要做一点编码:

You will still need to do a little coding too:

  1. 定义正态分布.
  2. 从中取样.

注意,您需要定义一次,然后采样,而不是重新定义.

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();                          
            }
}

在您的应用程序开始时初始化类以定义法线,然后每次只需调用 getRandom() 即可从中采样.您还可以将该类添加到现有的接口中.

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.

这篇关于c#中使用普通dstr的随机数生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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