第一步:产生很多(0,1)上的均匀分布随机数(可以查表,不过一般的软件可以直接调)设为x1,x2,x3,x4,x5....
第二步:假设需要模拟的泊松分布参数为λ,计算出e^(-λ),
第三步:取满足x1* x2* ...* xk >= e^(-λ) > x1* x2* ...*x(k+1)
中的K为产生的第一个随机数,然后把上面用过的k+1个数去掉,又重复上面的步骤就可以了。
比如 x1*x2 >= e^(-λ),但是x1*x2*x3 < e^(-λ),那么产生的第一个随机数就为2, 然后又从x4乘起,重复上面的步骤,产生第二个随机数。
#include
#include
#include
#include
double Rand()
{
return (double)(rand() & 0xFFFF) / (double)0x10000;
}
int Prand(double n)
{
double t=1, x = Rand();
int y = 0;
do
{
t *= x;
x = Rand();
if(t >= n) y ++;
}
while(t >= n);
return y;
}
void main()
{
int table[5][4];
double n;
srand(time(NULL));
printf("please input number");
scanf("%lf", &n);
n = exp(-n);
for(int i=0;i<5;i++) for(int j=0;j<4;j++) table[i][j] = Prand(n);
for(i=0;i<5;i++)
{
for(int j=0;j<4;j++) printf("%d\t", table[i][j]);
printf("\n");
}
}
觉得你的这种说法在概率里面来说很诡异。
硬要生成的话:
N = poissrnd(5,5,4)/10
N =
0.7000 0.7000 0.4000 0.6000
0.9000 0.7000 1.0000 0.6000
0.3000 0.6000 0.7000 0.4000
0.3000 0.4000 0.5000 0.5000
0.3000 0.5000 0.3000 0.8000
rand(5,4)