C#产生正态分布、泊松分布、指数分布、负指数分布随机数(原创)
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
????????????{
????????????????x = AverageRandom(min, max);
????????????????y = Normal(x, miu, sigma);
????????????????dScope = AverageRandom(0, Normal(miu, miu, sigma));
????????????} while (dScope > y);
????????????return x;
????????}
????????/// <summary>
????????/// 指數分布隨機數
????????/// </summary>
????????/// <param name="const_a"></param>
????????/// <returns></returns>
????????public??double RandExp(double const_a)//const_a是指數分布的參數λ
????????{
????????????Random rand = new Random(Guid.NewGuid().GetHashCode());
????????????double p;
????????????double temp;
????????????if (const_a != 0)
????????????????temp = 1 / const_a;
????????????else
????????????????throw new System.InvalidOperationException("除數不能為零!不能產生參數為零的指數分布!");
????????????double randres;
????????????while (true) //用于產生隨機的密度,保證比參數λ小
????????????{
????????????????p = rand.NextDouble();
????????????????if (p < const_a)
????????????????????break;
????????????}
????????????randres = -temp * Math.Log(temp * p, Math.E);
????????????return randres;
????????}
?
????????/// <summary>
????????/// 負指數分布隨機數產生
????????/// </summary>
????????/// <param name="lam">參數</param>
????????/// <returns></returns>
????????Random ran;
????????public Model()
????????{
????????????ran = new Random();
????????}
????????public double ngtIndex(double lam)
????????{
????????????double dec = ran.NextDouble();
????????????while (dec == 0)
????????????????dec = ran.NextDouble();
????????????return -Math.Log(dec) / lam;
????????}
?
????????/// <summary>
????????/// 泊松分布產生
????????/// </summary>
????????/// <param name="lam">參數</param>
????????/// <param name="time">時間</param>
????????/// <returns></returns>
????????public double poisson(double lam, double time)
????????{
????????????int count = 0;
????????????while (true)
????????????{
????????????????time -= ngtIndex(lam);
????????????????if (time > 0)
????????????????????count++;
????????????????else
????????????????????break;
????????????}
????????????return count;
????????}
?
????????}
????}
總結
以上是生活随笔為你收集整理的C#产生正态分布、泊松分布、指数分布、负指数分布随机数(原创)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站建设方案:网站建设合同之撰写注意点
- 下一篇: 我推荐支持