http://blog.sina.com.cn/s/blog_76c31b8e0100qskf.html
在编程过程中,由于数据仿真模拟的需要,我们经常需要产生一些随机数,在C#中,产生一般随机数用Random即可,但是,若要产生服从特定分布的随机数,就需要一定的算法来支持了,为了方便广大编程人员,我将我再做项目过程中用到的产生服从正态分布、泊松分布、指数分布以及负指数分布随机数的方法与大家共享,希望会有所帮助!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace PZ
{
class Model
{
/// <summary>
/// 正态分布随机数
/// </summary>
const int N = 100;
const int MAX = 50;
const double MIN = 0.1;
const int MIU = 40;
const int SIGMA = 1;
static Random aa = new Random((int)(DateTime.Now.Ticks / 10000));
public double AverageRandom(double min, double max)//产生(min,max)之间均匀分布的随机数
{
int MINnteger = (int)(min * 10000);
int MAXnteger = (int)(max * 10000);
int resultInteger = aa.Next(MINnteger, MAXnteger);
return resultInteger / 10000.0;
}
public double Normal(double x, double miu, double sigma) //正态分布概率密度函数
{
return 1.0 / (x * Math.Sqrt(2 * Math.PI) * sigma) * Math.Exp(-1 * (Math.Log(x) – miu) * (Math.Log(x) – miu) / (2 * sigma * sigma));
}
public double Random_Normal(double miu, double sigma, double min, double max)//产生正态分布随机数
{
double x;
double dScope;
double y;
do
{