//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX_VALUES 6000

#include <vcl.h>
#pragma hdrstop
////////////////////////使用前 必須對srand(讓其對時間有變化)
///////////////////////n不能等於1

float rand_normal1(void);
void rand_normal2(float* n1, float* n2);

//---------------------------------------------------------------------------
/*
Normal.c-Standard Normal Random Numbers
This program generates random numbers from a standard
normal distribution using a) the central limit theorem,
and b) the Box-Muller algorithm.
*/

#pragma argsused
int main(int argc, char* argv[])
{
        float x[MAX_VALUES];    //array containing random nubers
        int n;          //number of random values required
        int i;          //loop counter
        int choice;             //algorithm to be used
        float sumx,sumxx;               //sum of x and x*x;
        float mean,std_dev;             //mean and standard deviation
        float temp;                     //temporary storage

        printf("\n STANDARD NORMAL RANDOM NUMBER GENERATOR \N");
        printf("\n Number of values required:");
        scanf("%d",&n);

        choice=0;
        while (choice<1 || choice >2)
        {
                printf("\n Select algorithm");
                printf("\n  \t 1 - Central Limit Theorem");
                printf("\n  \t 2 - Box Muller");
                printf("\n Your choice (1 or 2):");
                scanf("%d",&choice);
        }
        srand((unsigned)time(NULL));

        switch (choice)
        {
                case 1:
                        for (i=0;i<n;++i)
                                x[i]=rand_normal1();
                break;
                case 2:
                       // for (i=0;i<n/2;i+=2)
                        for (i=0;i<n;i+=2)
                                rand_normal2(&x[i],&x[i+1]);
                        if (n%2!=0)
                                rand_normal2(&x[n-1],&temp);
                                //rand_normal2(&x[n],&temp);
                break;
        }
        //print random number - 6 per line
        for (i=0;i<n;++i)
        {
                sumx += x[i];
                sumxx+= x[i]*x[i];
                printf("%10.6f",x[i]);
        }

        //compute mean value and standard deviation
        sumx=0.0;
        sumxx=0.0;

        for (i=0;i<n;++i)
        {
                sumx += x[i];
                sumxx+= x[i]*x[i];
        }
        mean=sumx/n;
        std_dev=sqrt((sumxx-n*(mean*mean))/(n-1));

        printf("\n\n Mean value =%f",mean);
        printf("\n Standard deviation = %f",std_dev);

        system("pause");
        return 0;
}
//---------------------------------------------------------------------------
/*
Function rand_normal1()
purpose:
Generates random numbers from the standard normal distribution using the Central Limit Theorem
Input Parameters:
None
Output Parameters:
result -standard normal random numbers
Calls:
rand() - C library random number generator
*/
float rand_normal1(void)
{
        int i;
        int temp;
        float result = -6.0;

        for (i=0;i<12;++i)
        {
                temp=rand();
                result+=((float)temp/RAND_MAX);
        }
        return (result);
}
/*Function:rand_normal2(0
Purpose:
Generates random numbers from the standard normal
distribution using the Box Muller algorithm
Input Parameters:
None
Output Parameters:
*n1,*n2 - standard normal random numbers
Calls:
rand() - C library random number generator
*/
void rand_normal2(float *n1,float *n2)
{
        float u1,u2,v1,v2,s;
        float temp1,temp2;

        do{
                u1=(float) rand()/RAND_MAX;
                u2=(float) rand()/RAND_MAX;
                v1=2.*u1-1.0;
                v2=2.*u2-1.0;
                s=v1*v1+v2*v2;
        }while( s>=1.0);

        temp1=log(s);
        temp2=sqrt(-2.0*temp1/s);
        *n1=v1*temp2;
        *n2=v2*temp2;
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 pipii 的頭像
    pipii

    Live Pipii

    pipii 發表在 痞客邦 留言(0) 人氣()