免责申明:上传资料仅供学习交流使用,禁止一切不正当使用行为,如有事故本人概不负责
1.均匀分布
double fun ( ) // X - U(0, 1)
{
double x;
x = ( rand( ) + 1.0 ) / ( RAND_MAX + 2.0 );
return x;
}
double fun (double a, double b) // X - U(a, b)
{
double x;
x = ( rand( ) + 1.0 ) / ( RAND_MAX + 2.0 );
x = (b-a)*x + a;
return x;
}
2.二项分布 X~ b(n, p)
int fun (int n, double p)
{
int i, x = 0;
double y;
for ( i = 0; i < n; i++ )
{
y = ( rand( ) + 1.0 ) / ( RAND_MAX + 2.0 );
if ( y <= p ) x++;
}
return x;
}
3.几何分布 X~ Ge(p)
int fun (double p)
{
int x = 0;
double y;
do {
x++;
y = ( rand( ) + 1.0 ) / ( RAND_MAX + 2.0 );
} while ( y > p );
return x;
}
4. 指数分布 X~ Exp(λ)
int fun (double lambda)
{
double x, y;
y = ( rand( ) + 1.0 ) / ( RAND_MAX + 2.0 );
x = log(1/(1-y)) / lambda; // 定理2.6.5
return x;
}
5. 泊松分布 X~ P(λ)
int fun (double lambda)
{
int i, x = 0, n = 1000;
double y, p = lambda / n; // 定理2.4.1 转换成二项分布
for ( i = 0; i < n; i++ )
{
y = ( rand( ) + 1.0 ) / ( RAND_MAX + 2.0 );
if ( y <= p ) x++;
}
return x;
}
6. 正态分布 X~ N(μ, σ^2)
int fun (double u, double sigma) // 中心极限定理4.4.1
{
int i, n = 100;
double x, y = 0;
for ( i = 0; i < n; i++ )
y += ( rand( ) + 1.0 ) / ( RAND_MAX + 2.0 );
y = (y – 0.5*n) / sqrt( n/12.0 );
x = u + sigma*y;
return x;
}
免责申明:上传资料仅供学习交流使用,禁止一切不正当使用行为,如有事故本人概不负责