C语言生成服从均匀分布, 瑞利分布, 莱斯分布, 高斯分布的随机数

C语言生成服从均匀分布, 瑞利分布, 莱斯分布, 高斯分布的随机数,第1张

C语言生成服从均匀分布, 瑞利分布, 莱斯分布, 高斯分布的随机数

用c语言 产生服从均匀分布, 瑞利分布,莱斯分布,高斯分布的随机数

  一,各个分布对应的基本含义:
  • 1. 均匀分布或称规则分布,顾名思义,均匀的,不偏差的。


    植物种群的个体是等距分布,或个体之间保持一定的均匀的间距。


  • 2. 高斯分布,  即正态分布(Normal distribution),也称“常态分布”,又名高斯分布(Gaussian distribution),最早由A.棣莫弗在求二项分布的渐近公式中得到。


    C.F.高斯在研究测量误差时从另一个角度导出了它。


    P.S.拉普拉斯和高斯研究了它的性质。


    [1]  是一个在数学、物理及工程等领域都非常重要的概率分布,在统计学的许多方面有着重大的影响力。


    正态曲线呈钟型,两头低,中间高,左右对称因其曲线呈钟形,因此人们又经常称之为钟形曲线。


    若随机变量X服从一个数学期望为μ、方差为σ^2的正态分布,记为N(μ,σ^2)。


    其概率密度函数为正态分布的期望值μ决定了其位置,其标准差σ决定了分布的幅度。


    当μ = 0,σ = 1时的正态分布是标准正态分布。


  • 3. 瑞利分布(Rayleigh Distribution):当一个随机二维向量的两个分量呈独立的、有着相同的方差的正态分布时,这个向量的模呈瑞利分布.
  • 4. 莱斯分布(Rice distribution或Rician distribution)是一种连续概率分布,以美国科学家斯蒂芬·莱斯(en:Stephen O. Rice)的名字命名。


    正弦波加窄带高斯过程的包络概率密度函数分布称为莱斯(Rice)密度函数,也称广义瑞利分布。


  二, 各个分布对应的随机变量产生的算法,        
 # include "stdio.h"
# include "math.h"
# include "stdlib.h"
# include "math.h"
# include "dos.h"
# define MAX_N /*这个值为N可以定义的最大长度*/
# define N /*产生随机序列的点数,注意不要大于MAX_N*/ /*1.产生均匀分布的随机变量*/
void randa(float *x,int num); /*2.产生瑞利分布的随机变量*/
void randr(float *x,int num); /*3.产生标准高斯分布的随机变量*/
void randn(float *x,int num); /*4.产生莱斯分布的随机变量*/
void randl(float *x, float a, float b, int num); void fshow(char *name,float *x,int num); /***************************************/
int main()
{ float x[N];
int i; // randa(&x,N); //均匀分布
// randr(&x,N); //瑞利分布
// randl(&x,10,10,N); //莱斯分布
randn(&x,N); //高斯分布 /*此时x[N]表示要生成N个服从xx分布的的数组*/ fshow("x",&x,N); /*显示该序列*/ getch();
return 0; }
/***************函数定义************************/ /*产生服从均匀分布的随机变量*/
void randa(float *x,int num)
{
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=;i<num;i++)
{
x[i]=rand();
x[i]=x[i]/;
}
}
/*产生服从瑞利分布的随机变量*/
void randr(float *x,int num)
{
float x1[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=;i<num;i++)
{
x1[i]=rand();
x[i]=x1[i]/;
x[i]=sqrt(-*log(x[i]));
} }
/*产生服从标准高斯分布的随机变量*/
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=;i<num;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/;
x2[i]=x2[i]/;
x[i]=sqrt(-*log(x1[i]))*cos(x2[i]*M_PI);
} }
/*产生服从莱斯分布的随机变量*/
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N];
float temp[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=;i<num;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/;
x2[i]=x2[i]/;
temp[i]=sqrt(-*log(x1[i]))*cos(x2[i]*M_PI);
x2[i]=sqrt(-*log(x1[i]))*sin(x2[i]*M_PI);
x1[i]=temp[i];
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]));
} } void fshow(char *name,float *x,int num)
{
int i,sign,L;
float temp;
printf("\n");
printf(name);
printf(" = ");
L=;
/*按照每行6个数据的格式显示*/
for(i=;i<num;i++)
{
temp=i/L;
sign=temp;
if((i-sign*L)==) printf("\n");
if(x[i]>) printf(" %f ",x[i]);
else printf("%f ",x[i]);
}
}

其他分布的详细介绍, 请戳这里:http://www.math.uah.edu/stat/special/index.html

国外知名网站给出的各种分布的曲线图(后台程序驱动):

http://www.math.uah.edu/stat/apps/SpecialSimulation.html

---OVER---

附录:   Cauchy 分布 随机数生成代码:

 import math
import random def cauchy(location, scale): # Start with a uniform random sample from the open interval (0, 1).
# But random() returns a sample from the half-open interval [0, 1).
# In the unlikely event that random() returns 0, try again. p = 0.0
while p == 0.0:
p = random.random() return location + scale*math.tan(math.pi*(p - 0.5))

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/586936.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存