test.c.bak

来自「产生一个满足泊松分布的随机序列信号。用于通信系统的信源部分」· BAK 代码 · 共 86 行

BAK
86
字号
//MultipleMethod mid mothed
//Possibility Possion Distribute by Distribute Rule
#include <stdio.h>
#include <math.h>

#define Lamda 3125
#define PI    3.1415926535


float Xn=12345;//Seed & Iter
float Rn;//Return Val
long  M;


void InitSeed(float inX0)
{
	Xn=inX0;
}

/*
	Xn+1=Lamda*Xn(mod M)
	Rn+1=Xn/M
*/

float MyRnd()
{
		Xn=fmod(Lamda*Xn,M);//here can's use % 
		Rn=-Xn/M;
		return Rn;
}

/*-------------------------------------*/

/*Sample Distribution:Norm,use select method.
	1.get rand1,rand2;
	2.u=2*r1-1,v=2*r2-1,w=u^2+v^2;
	3.if w>1 goto 1
	4.else	x=u[(-ln(w)/w)]^(1/2);y=v[(-lnw)/w]^(1/2)
				
*/
float funPossion(float a)
{		
		float temp;
		int count;
		
		count=-1;
		do
		{	
			temp=MyRnd();
			count++;	
		}while(temp>exp(-a));
		
		return count;
}

/*-------------------------------------*/



int main()
{
		int i;
		FILE * debugFile;
		
		if((debugFile=fopen("outputData.txt","w"))==NULL)
		  {
            fprintf(stderr,"open file error!");
            return -1;
          }    
		
        printf("\n");
	
		//a proper argu number
		
		M=pow(2,35)-31;
	
		for(i=0;i<300;i++)
		{
        	fprintf(stdout,"%f ",funPossion(3.5));
			fprintf(debugFile,"%f ",funPossion(3.5));
		}			
		getchar();
		
		return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?