📄 rvg_可用版.c
字号:
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "string.h"
#include "windows.h"
#define znew (z=36969*(z&65535)+(z>>16))
#define wnew (w=18000*(w&65535)+(w>>16))
#define MWC ((znew<<16)+wnew )
#define SHR3 (jsr^=(jsr<<17), jsr^=(jsr>>13), jsr^=(jsr<<5))
#define CONG (jcong=69069*jcong+1234567)
#define KISS ((MWC^CONG)+SHR3)
#define NUM 200000
typedef unsigned long UL;
static UL z=362436069, w=521288629, jsr=123456789, jcong=380116160;
void settable(UL i1,UL i2,UL i3,UL i4)
{ z=i1;
w=i2,
jsr=i3;
jcong=i4;
}
double normal(float u,float o2) //Use sum_of_12 method
{ int m;
UL k;
double o,iid[12],nor=0.0;
o=sqrt((double)o2);
for(m=0;m<12;m++)
{
k=KISS;
iid[m]=(double)(k-1372460312U)/4294967296;
nor+=iid[m];
}
return(u+o*(nor-6.0));
}
void main()
{UL i,j,k;
int option;
float u,o2,lamda;
double gau,gau2,ray;
double a,b,Ui,mul;
char name[20];
FILE *out;
settable(12345,65435,34221,12345);
while(1)
{
L1:printf("\t\t============================================\n");
printf("\t\t|| Random Variates Generator ||\n");
printf("\t\t|| ||\n");
printf("\t\t||1.Gaussian distributed random variates ||\n");
printf("\t\t||2.Rayleigh distributed random variates ||\n");
printf("\t\t||3.Poisson distributed random variates ||\n");
printf("\t\t||4.Exit ||\n");
printf("\t\t|| ||\n");
printf("\t\t============================================\n");
printf("\n\t\t Please select: ");
scanf("%d",&option);
switch(option)
{
case 1:{
printf("Please input the expectation(u) and variance(o2) of the Gaussian distribution :");
printf("\nu=");
scanf("%f",&u);
printf("o2=");
scanf("%f",&o2);
printf("Please input the file's name of the data:");
scanf("%s",name);
strcat(name,".txt");
out=fopen(name,"w");
for(i=0;i<NUM;i++)
{
gau=normal(u,o2);
fprintf(out,"%f\n",gau);
}
fprintf(out,"\n");
fclose(out);
getch();
break;
}
case 2:{
printf("Please input the expectation(u) and variance(o2) of the Rayleigh distribution :");
printf("\nu=");
scanf("%f",&u);
printf("o2=");
scanf("%f",&o2);
printf("Please input the file's name of the data:");
scanf("%s",name);
strcat(name,".txt");
out=fopen(name,"w");
for(i=0;i<NUM;i++)
{
gau=normal(u,o2);
gau2=normal(u,o2);
ray=sqrt(gau*gau+gau2*gau2);
fprintf(out,"%f\n",ray);
}
fprintf(out,"\n");
fclose(out);
getch();
break;
}
case 3:{
printf("Please input the paramater(lamda) of the Poission distribution :");
printf("\nlamda=");
scanf("%f",&lamda);
printf("Please input the file's name of the data:");
scanf("%s",name);
strcat(name,".txt");
out=fopen(name,"w");
lamda=-lamda;
mul=1;
a=exp((double)lamda);
for(i=0;i<NUM;i++)
{b=1;
j=0;
L2: k=KISS;
Ui=(double)(k-1372460312U)/4294967296;
b=b*Ui;
if(b<a)
fprintf(out,"%u\n",j);
else
{
j++;
goto L2;
}
mul=mul*Ui;
if(mul<a)
{
fprintf(out,"%u\n",j);
mul=1;
}
}
fprintf(out,"\n");
fclose(out);
getch();
break;
}
case 4: {exit(0);}
default:{
printf("Error input");
getch();
goto L1;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -