zoukankan      html  css  js  c++  java
  • 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)密度函数,也称广义瑞利分布。
     
    二, 各个分布对应的随机变量产生的算法,
         
     
      1 # include "stdio.h"
      2 # include "math.h"
      3 # include "stdlib.h"
      4 # include "math.h"
      5 # include "dos.h"
      6 # define MAX_N 3000   /*这个值为N可以定义的最大长度*/
      7 # define N 100        /*产生随机序列的点数,注意不要大于MAX_N*/
      8 
      9 /*1.产生均匀分布的随机变量*/
     10 void randa(float *x,int num); 
     11 
     12 /*2.产生瑞利分布的随机变量*/
     13 void randr(float *x,int num); 
     14 
     15 /*3.产生标准高斯分布的随机变量*/
     16 void randn(float *x,int num);
     17 
     18 /*4.产生莱斯分布的随机变量*/
     19 void randl(float *x, float a, float b, int num);
     20 
     21 void fshow(char *name,float *x,int num);
     22 
     23 /***************************************/
     24 int main()
     25 {
     26 
     27    float x[N];
     28    int i;
     29 
     30   // randa(&x,N);             //均匀分布
     31   // randr(&x,N);             //瑞利分布
     32   // randl(&x,10,10,N);       //莱斯分布
     33      randn(&x,N);             //高斯分布
     34 
     35 /*此时x[N]表示要生成N个服从xx分布的的数组*/
     36 
     37 
     38    fshow("x",&x,N); /*显示该序列*/
     39 
     40    getch();
    return 0;
    41 42 } 43 /***************函数定义************************/ 44 45 /*产生服从均匀分布的随机变量*/ 46 void randa(float *x,int num) 47 { 48 int i; 49 struct time stime; 50 unsigned seed; 51 gettime(&stime); 52 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 53 srand(seed); 54 for(i=0;i<num;i++) 55 { 56 x[i]=rand(); 57 x[i]=x[i]/32768; 58 } 59 } 60 /*产生服从瑞利分布的随机变量*/ 61 void randr(float *x,int num) 62 { 63 float x1[MAX_N]; 64 int i; 65 struct time stime; 66 unsigned seed; 67 gettime(&stime); 68 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 69 srand(seed); 70 for(i=0;i<num;i++) 71 { 72 x1[i]=rand(); 73 x[i]=x1[i]/32768; 74 x[i]=sqrt(-2*log(x[i])); 75 } 76 77 } 78 /*产生服从标准高斯分布的随机变量*/ 79 void randn(float *x,int num) 80 { 81 float x1[MAX_N],x2[MAX_N]; 82 int i; 83 struct time stime; 84 unsigned seed; 85 gettime(&stime); 86 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 87 srand(seed); 88 for(i=0;i<num;i++) 89 { 90 x1[i]=rand(); 91 x2[i]=rand(); 92 x1[i]=x1[i]/32768; 93 x2[i]=x2[i]/32768; 94 x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI); 95 } 96 97 } 98 /*产生服从莱斯分布的随机变量*/ 99 void randl(float *x, float a, float b, int num) 100 { 101 float x1[MAX_N],x2[MAX_N]; 102 float temp[MAX_N]; 103 int i; 104 struct time stime; 105 unsigned seed; 106 gettime(&stime); 107 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 108 srand(seed); 109 for(i=0;i<num;i++) 110 { 111 x1[i]=rand(); 112 x2[i]=rand(); 113 x1[i]=x1[i]/32768; 114 x2[i]=x2[i]/32768; 115 temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI); 116 x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI); 117 x1[i]=temp[i]; 118 x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i])); 119 } 120 121 } 122 123 void fshow(char *name,float *x,int num) 124 { 125 int i,sign,L; 126 float temp; 127 printf(" "); 128 printf(name); 129 printf(" = "); 130 L=6; 131 /*按照每行6个数据的格式显示*/ 132 for(i=0;i<num;i++) 133 { 134 temp=i/L; 135 sign=temp; 136 if((i-sign*L)==0) printf(" "); 137 if(x[i]>0) printf(" %f ",x[i]); 138 else printf("%f ",x[i]); 139 } 140 }

     

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

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

     ---OVER---

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

     1 import math
     2 import random
     3 
     4 def cauchy(location, scale):
     5 
     6 # Start with a uniform random sample from the open interval (0, 1).
     7 # But random() returns a sample from the half-open interval [0, 1).
     8 # In the unlikely event that random() returns 0, try again.
     9 
    10 p = 0.0
    11 while p == 0.0:
    12 p = random.random()
    13 
    14 return location + scale*math.tan(math.pi*(p - 0.5))
  • 相关阅读:
    XAML学习笔记之Layout(五)——ViewBox
    XAML学习笔记——Layout(三)
    XAML学习笔记——Layout(二)
    XAML学习笔记——Layout(一)
    从0开始搭建SQL Server 2012 AlwaysOn 第三篇(安装数据,配置AlwaysOn)
    从0开始搭建SQL Server 2012 AlwaysOn 第二篇(配置故障转移集群)
    从0开始搭建SQL Server 2012 AlwaysOn 第一篇(AD域与DNS)
    Sql Server 2012 事务复制遇到的问题及解决方式
    Sql Server 2008R2升级 Sql Server 2012 问题
    第一次ACM
  • 原文地址:https://www.cnblogs.com/tsingke/p/5866672.html
Copyright © 2011-2022 走看看