Common pseudo-random numbers are often generated.

Source: Internet
Author: User
Tags random seed

# Include "stdlib. H"
# Include "stdio. H"
# Include "math. H"


Double uniform (double A, double B, long int * seed );
Double Gauss (double mean, double Sigma, long int * seed );
Double exponent (double beta, long int * seed );
Double Laplace (double beta, long int * seed );
Double enumeration (double Sigma, long int * seed );
Double Weibo (double A, double B, long int * seed );
Int bn (Double P, long int * seed );
Int Bin (int n, Double P, long int * seed );
Int Poisson (double lambda, long int * seed );

Void main ()
{
Double A, B, X, mean;
Int I, J;
Long int S;

A = 4;
B = 0.7;
S = 13579;
Mean = 0;
For (I = 0; I <10; I ++)
{
For (j = 0; j <5; j ++)
{
X = Poisson (A, & S );
Mean + = X;
Printf ("%-13.7f", X );
}
Printf ("/N ");
}
Mean/= 50;
Printf ("average value: %-13.7f/N", mean );
}

/*************************************** ****************************
* Calculate the even distribution on [a, B]
* Input: A -- double-precision real-type variable, giving the lower limit of the Interval
* B -- double-precision real-type variable, giving the upper limit of the Interval
* Seed -- a long integer pointer variable. * seed is the seed of a random number.
**************************************** ****************************/
Double uniform (double A, double B, long int * seed)
{
Double T;
* Seed = 2045 * (* seed) + 1;
* Seed = * seed-(* seed/1048576) * 1048576;
T = (* seed)/1048576.0;
T = a + (B-a) * t;

Return (t );
}

/*************************************** ****************************
* Normal Distribution
* Input: Mean -- double-precision real-type variable, mean of Normal Distribution
* Sigma -- mean variance of normal distribution of Double Precision Real Variables
* Seed -- a long integer pointer variable. * seed is the seed of a random number.
**************************************** ****************************/
Double Gauss (double mean, double Sigma, long int * seed)
{
Int I;
Double X, Y;
For (x = 0, I = 0; I <12; I ++)
X + = uniform (0.0, 1.0, seed );
X = x-6.0;
Y = mean + x * sigma;

Return (y );
}

/*************************************** ****************************
* Exponential Distribution
* Input: beta-Exponential Distribution Mean
* Seed -- Seed
**************************************** ***************************/
Double exponent (double beta, long int * seed)
{
Double U, X;
U = uniform (0.0, 1.0, seed );
X =-beta * log (U );

Return (X );
}

/*************************************** ****************************
* Laplace Random Distribution
* Beta -- parameters of Laplace Distribution
** Seed -- Random Seed
**************************************** ***************************/
Double Laplace (double beta, long int * seed)
{
Double u1, U2, X;

U1 = uniform (0., 1., seed );
U2 = uniform (0., 1., seed );
If (U1 <= 0.5)
X =-beta * log (1.-U2 );
Else
X = beta * log (U2 );

Return (X );
}

/*************************************** *****************************
* Ruili Distribution
*
**************************************** ****************************/
Double enumeration (double Sigma, long int * seed)
{
Double U, X;
U = uniform (0., 1., seed );
X =-2.0 * log (U );
X = Sigma * SQRT (X );
Return (X );
}

/*************************************** *********************************/
/* Weber Distribution */
/*************************************** *********************************/
Double Weibo (double A, double B, long int * seed)
{
Double U, X;

U = uniform (0.0, 1.0, seed );
U =-log (U );
X = B * POW (u, 1.0/);

Return (X );
}

/*************************************** *********************************/
/* Benuli distribution */
/*************************************** *********************************/
Int bn (Double P, long int * seed)
{
Int X;
Double U;
U = uniform (0.0, 1.0, seed );
X = (U <= P )? 1:0;
Return (X );
}

/*************************************** *********************************/
/* Binary distribution */
/*************************************** *********************************/
Int Bin (int n, Double P, long int * seed)
{
Int I, X;
For (x = 0, I = 0; I <n; I ++)
X + = BN (p, seed );
Return (X );
}

/*************************************** *********************************/
/* Poisson distribution */
/*************************************** *********************************/
Int Poisson (double lambda, long int * seed)
{
Int I, X;
Double A, B, U;

A = exp (-lambda );
I = 0;
B = 1.0;
Do {
U = uniform (0.0, 1.0, seed );
B * = u;
I ++;
} While (B> = );
X = I-1;
Return (X );
}

 

/*************************************** *********************************
* Discrete Fourier transformation and Inverse Transformation
* Input: X -- real part of the data to be transformed
* Y -- the virtual part of the data to be transformed
* A -- real part of the Transformation Result
* B -- virtual part of the Transformation Result
* N -- Data Length
* When sign -- Sign = 1, the discrete Fourier positive transformation is calculated. When Sign =-1, the discrete Fourier inverse transformation is calculated.
**************************************** ********************************/
Void DFT (Double X [], Double Y [], double A [], double B [], int N, int sign)
{
Int I, K;
Double C, D, Q, W, S;

Q= 6.28318530718/N;
For (k = 0; k <n; k ++)
{
W = K * q;
A [k] = B [k] = 0.0;
For (I = 0; I <n; I ++)
{
D = I * W;
C = cos (d );
S = sin (d) * sign;
A [k] + = C * x+ S * y;
B [k] + = C * y-S * x;
}
}
If (Sign =-1)
{
C = 1.0/N;
For (k = 0; k <n; k ++)
{
A [k] = C * A [k];
B [k] = C * B [k];
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.