Use C language to implement the distribution functions of the Swiss distribution, les distribution, and Gaussian distribution

Source: Internet
Author: User
Program 1 is what you want. # Include "stdio. H "# include" math. H "# include" stdlib. H "# include" math. H "# include" dos. H "# define max_n 3000/* this value is the maximum length that N can define */# define n 100/* number of points that generate random sequences, be sure not to generate a random variable */void Randa (float * X, int num) with a uniform distribution greater than max_n ); /* generate random variable */void randroid (float * X, int num);/* generate random variable of standard Gaussian distribution */void randn (float * X, int num);/* random variable that generates the number of Les distribution */void randl (float * X, float a, float B, int num); void fshow (char * Name, float * X, I NT num); main () {float X [N]; int I;/* Randa (& X, n); randroid (& X, n); randl (& X, 10, 10, n); */randn (& X, N ); /* at this time, X [N] is the sequence of the required Gaussian distribution * // * shows the sequence */fshow ("X", & X, N ); getch ();} void Randa (float * X, int num) {int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x [I] = rand (); X [I] = x [I]/32768;} void randroid (float * X, int num) {float X1 [max_n ]; Int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); X [I] = x1 [I]/32768; X [I] = SQRT (-2 * log (X [I]);} void randn (float * X, int num) {float X1 [max_n], X2 [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); X1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/32768; X [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi) ;}} void randl (float * X, float a, float B, int num) {float X1 [max_n], X2 [max_n]; float temp [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); x1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/ 32768; temp [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi ); x2 [I] = SQRT (-2 * log (x1 [I]) * sin (X2 [I] * m_pi); X1 [I] = temp [I]; X [I] = SQRT (a + X1 [I]) * (a + X1 [I]) + (B + x2 [I]) * (B + x2 [I]) ;}} void fshow (char * Name, float * X, int num) {int I, sign, L; float temp; printf ("\ n"); printf (name); printf ("="); L = 6; /* display 6 data entries in each row */for (I = 0; I <num; I ++) {temp = I/L; Sign = temp; if (I-sign * L) = 0) printf ("\ n"); If (X [I]> 0) printf ("% F ", X [I] ); Else printf ("% F", X [I]);} the program below Program 2 adds the graphic display effect, so it is more intuitive, you can refer. /* The author leo_nanjing time 2008.5.10 function generates random variables of various distributions and displays */# include "stdio. H "# include" math. H "# include" graphics. H "# include" math. H "# include" dos. H "# define max_n 3000 # define n 1000 void Randa (float * X, int num); void randroid (float * X, int num); void randn (float * X, int num); void randl (float * X, float a, float B, int num); void fshow (char * Name, float * X, int num ); /* for graphic display */void init_graphic (unsigned color); void plotxy (float * X, float * y, int num, int mode ); void plot (float * y, int num, int mode); float max (float * X, int num); float min (float * X, int num ); /* draw the Distribution Function Curve of the random sequence */void plotpdf (float * X, int num, int part, int mode); main () {float X [N]; int I; randn (& X, n); fshow ("X", & X, n); getch (); /* The following figure shows the graphic display part */init_graphic (0);/* display random sequence */plot (& X, N, 1); getch (); /* display its distribution function */plotpdf (& X, N, 20, 0); getch ();} void Randa (float * X, int num) {int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x [I] = rand (); X [I] = x [I]/32768;} void randroid (float * X, int num) {float X1 [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); X [I] = x1 [I]/32768; X [I] = SQRT (-2 * log (X [I]);} void randn (float * X, int num) {float X1 [max_n], X2 [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); x1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/32768; X [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi) ;}} void randl (float * X, float a, float B, int num) {float X1 [max_n], X2 [max_n]; float temp [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); x1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/32768; temp [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi ); x2 [I] = SQRT (-2 * log (x1 [I]) * sin (X2 [I] * m_pi); X1 [I] = temp [I]; X [I] = SQRT (a + X1 [I]) * (a + X1 [I]) + (B + x2 [I]) * (B + x2 [I]) ;}} void fshow (char * Name, float * X, int num) {int I, sign, L; float temp; printf ("\ n"); printf (name); printf ("="); L = 6; for (I = 0; I <num; I ++) {temp = I/L; Sign = temp; If (I-sign * L) = 0) printf ("\ n "); if (X [I]> 0) printf ("% F", X [I]); else printf ("% F", X [I]);} /* the following functions are graphic display */void init_graphic (unsigned color) {int graphicdriver, graphicmode; graphicdriver = detect; graphicmode = 1; initgraph (& graphicdriver, & graphicmode, "E: \ turboc2 \"); setbkcolor (color);} void plotxy (float * X, float * y, int num, int mode) {int I; float max_x, max_y, min_x, min_y; float x0, y0, X1, Y1; clrscr (0); cleardevice (); setbkcolor (0); max_x = max (X, num); max_y = max (Y, num); min_x = min (x, num); min_y = min (Y, num); setlinestyle (0, 2, 1 ); line (65,445, 65,445,575,445); line (575,445); setlinestyle (, 1); line (, 35,); line (,); setlinestyle (, 1 ); if (max_x = min_x) X0 = 320; else X0 = (X [0]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) y0 = 240; else Y0 = 480-(Y [0]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x0, y0, 2); for (I = 1; I <num; I ++) {If (max_x = min_x) x1 = 320; else X1 = (X [I]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) Y1 = 240; else Y1 = 480-(Y [I]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x1, Y1, 2 ); line (x0, y0, X1, Y1); X0 = x1; Y0 = Y1;} printf ("\ n"); printf ("% F", max_y ); printf ("\ n"); printf ("\ n "); printf ("% F", (max_y + min_y)/2 ); printf ("\ n"); printf ("\ n "); printf ("% F", min_y); printf ("\ n % F", min_x); printf (""); printf ("% F", (max_x + min_x) /2); printf (""); printf ("% F", max_x);} void plot (float * y, int num, int mode) {int I; float max_x, max_y, min_x, min_y; float x0, y0, X1, Y1; float X [max_n]; clrscr (0); cleardevice (); setbkcolor (0 ); for (I = 0; I <num; I ++) x [I] = I + 1; max_x = max (x, num); max_y = max (Y, num); min_x = min (x, num); min_y = min (Y, num); setlinestyle (65,445, 1); line (65,445,575,445,); line ); setlinestyle (575,445, 1); line (320, 35,); line (,); setlinestyle (, 1); If (max_x = min_x) X0 =; else X0 = (X [0]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) Y0 = 240; else Y0 = 480-(Y [0]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x0, y0, 2 ); for (I = 1; I <num; I ++) {If (max_x = min_x) x1 = 320; else X1 = (X [I]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) Y1 = 240; else Y1 = 480-(Y [I]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x1, Y1, 2); line (x0, y0, X1, Y1); X0 = x1; Y0 = Y1 ;} printf ("\ n"); printf ("% F", max_y ); printf ("\ n"); printf ("\ n "); printf ("% F", (max_y + min_y)/2 ); printf ("\ n"); printf ("\ n "); printf ("% F", min_y); printf ("\ n % F", min_x); printf (""); printf ("% F", (max_x + min_x) /2); printf (""); printf ("% F", max_x);} void plotpdf (float * X, int num, int part, int Mode) {int I, j; float max_x, min_x, round, deltax, up, down, sum; float XL [max_n], yl [max_n]; sum = 0; max_x = max (x, num); min_x = min (x, num); Round = max_x-min_x; deltax = round/part; XL [0] = min_x; for (I = 1; I <= part; I ++) {XL [I] = min_x + deltax * I; yl [I-1] = 0; up = XL [I]; down = XL [I-1]; for (j = 0; j <num; j ++) {If (X [J] <up) & (X [J]> = down) YL [I-1] = yl [I-1] + 1;} yl [I-1] = yl [I-1]/num/deltax ;} for (I = 0; I <part; I ++) sum = sum + yl [I]; plotxy (& XL, & YL, part, mode );} float max (float * X, int num) {int I; float Max; max = x [0]; for (I = 1; I <num; I ++) {If (X [I]> MAX) max = x [I];} return Max;} float min (float * X, int num) {int I; float min; min = x [0]; for (I = 1; I <num; I ++) {If (X [I] <min) min = x [I];} return min ;}
Program 1 is what you want. # Include "stdio. H "# include" math. H "# include" stdlib. H "# include" math. H "# include" dos. H "# define max_n 3000/* this value is the maximum length that N can define */# define n 100/* number of points that generate random sequences, be sure not to generate a random variable */void Randa (float * X, int num) with a uniform distribution greater than max_n ); /* generate random variable */void randroid (float * X, int num);/* generate random variable of standard Gaussian distribution */void randn (float * X, int num);/* random variable that generates the number of Les distribution */void randl (float * X, float a, float B, int num); void fshow (char * Name, float * X, I NT num); main () {float X [N]; int I;/* Randa (& X, n); randroid (& X, n); randl (& X, 10, 10, n); */randn (& X, N ); /* at this time, X [N] is the sequence of the required Gaussian distribution * // * shows the sequence */fshow ("X", & X, N ); getch ();} void Randa (float * X, int num) {int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x [I] = rand (); X [I] = x [I]/32768;} void randroid (float * X, int num) {float X1 [max_n ]; Int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); X [I] = x1 [I]/32768; X [I] = SQRT (-2 * log (X [I]);} void randn (float * X, int num) {float X1 [max_n], X2 [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); X1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/32768; X [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi) ;}} void randl (float * X, float a, float B, int num) {float X1 [max_n], X2 [max_n]; float temp [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); x1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/ 32768; temp [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi ); x2 [I] = SQRT (-2 * log (x1 [I]) * sin (X2 [I] * m_pi); X1 [I] = temp [I]; X [I] = SQRT (a + X1 [I]) * (a + X1 [I]) + (B + x2 [I]) * (B + x2 [I]) ;}} void fshow (char * Name, float * X, int num) {int I, sign, L; float temp; printf ("\ n"); printf (name); printf ("="); L = 6; /* display 6 data entries in each row */for (I = 0; I <num; I ++) {temp = I/L; Sign = temp; if (I-sign * L) = 0) printf ("\ n"); If (X [I]> 0) printf ("% F ", X [I] ); Else printf ("% F", X [I]);} the program below Program 2 adds the graphic display effect, so it is more intuitive, you can refer. /* The author leo_nanjing time 2008.5.10 function generates random variables of various distributions and displays */# include "stdio. H "# include" math. H "# include" graphics. H "# include" math. H "# include" dos. H "# define max_n 3000 # define n 1000 void Randa (float * X, int num); void randroid (float * X, int num); void randn (float * X, int num); void randl (float * X, float a, float B, int num); void fshow (char * Name, float * X, int num ); /* for graphic display */void init_graphic (unsigned color); void plotxy (float * X, float * y, int num, int mode ); void plot (float * y, int num, int mode); float max (float * X, int num); float min (float * X, int num ); /* draw the Distribution Function Curve of the random sequence */void plotpdf (float * X, int num, int part, int mode); main () {float X [N]; int I; randn (& X, n); fshow ("X", & X, n); getch (); /* The following figure shows the graphic display part */init_graphic (0);/* display random sequence */plot (& X, N, 1); getch (); /* display its distribution function */plotpdf (& X, N, 20, 0); getch ();} void Randa (float * X, int num) {int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x [I] = rand (); X [I] = x [I]/32768;} void randroid (float * X, int num) {float X1 [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); X [I] = x1 [I]/32768; X [I] = SQRT (-2 * log (X [I]);} void randn (float * X, int num) {float X1 [max_n], X2 [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); x1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/32768; X [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi) ;}} void randl (float * X, float a, float B, int num) {float X1 [max_n], X2 [max_n]; float temp [max_n]; int I; struct time stime; unsigned seed; gettime (& stime); seed = stime. ti_hund * stime. ti_min * stime. ti_hour; srand (SEED); for (I = 0; I <num; I ++) {x1 [I] = rand (); x2 [I] = rand (); x1 [I] = x1 [I]/32768; x2 [I] = x2 [I]/32768; temp [I] = SQRT (-2 * log (x1 [I]) * Cos (X2 [I] * m_pi ); x2 [I] = SQRT (-2 * log (x1 [I]) * sin (X2 [I] * m_pi); X1 [I] = temp [I]; X [I] = SQRT (a + X1 [I]) * (a + X1 [I]) + (B + x2 [I]) * (B + x2 [I]) ;}} void fshow (char * Name, float * X, int num) {int I, sign, L; float temp; printf ("\ n"); printf (name); printf ("="); L = 6; for (I = 0; I <num; I ++) {temp = I/L; Sign = temp; If (I-sign * L) = 0) printf ("\ n "); if (X [I]> 0) printf ("% F", X [I]); else printf ("% F", X [I]);} /* the following functions are graphic display */void init_graphic (unsigned color) {int graphicdriver, graphicmode; graphicdriver = detect; graphicmode = 1; initgraph (& graphicdriver, & graphicmode, "E: \ turboc2 \"); setbkcolor (color);} void plotxy (float * X, float * y, int num, int mode) {int I; float max_x, max_y, min_x, min_y; float x0, y0, X1, Y1; clrscr (0); cleardevice (); setbkcolor (0); max_x = max (X, num); max_y = max (Y, num); min_x = min (x, num); min_y = min (Y, num); setlinestyle (0, 2, 1 ); line (65,445, 65,445,575,445); line (575,445); setlinestyle (, 1); line (, 35,); line (,); setlinestyle (, 1 ); if (max_x = min_x) X0 = 320; else X0 = (X [0]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) y0 = 240; else Y0 = 480-(Y [0]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x0, y0, 2); for (I = 1; I <num; I ++) {If (max_x = min_x) x1 = 320; else X1 = (X [I]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) Y1 = 240; else Y1 = 480-(Y [I]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x1, Y1, 2 ); line (x0, y0, X1, Y1); X0 = x1; Y0 = Y1;} printf ("\ n"); printf ("% F", max_y ); printf ("\ n"); printf ("\ n "); printf ("% F", (max_y + min_y)/2 ); printf ("\ n"); printf ("\ n "); printf ("% F", min_y); printf ("\ n % F", min_x); printf (""); printf ("% F", (max_x + min_x) /2); printf (""); printf ("% F", max_x);} void plot (float * y, int num, int mode) {int I; float max_x, max_y, min_x, min_y; float x0, y0, X1, Y1; float X [max_n]; clrscr (0); cleardevice (); setbkcolor (0 ); for (I = 0; I <num; I ++) x [I] = I + 1; max_x = max (x, num); max_y = max (Y, num); min_x = min (x, num); min_y = min (Y, num); setlinestyle (65,445, 1); line (65,445,575,445,); line ); setlinestyle (575,445, 1); line (320, 35,); line (,); setlinestyle (, 1); If (max_x = min_x) X0 =; else X0 = (X [0]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) Y0 = 240; else Y0 = 480-(Y [0]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x0, y0, 2 ); for (I = 1; I <num; I ++) {If (max_x = min_x) x1 = 320; else X1 = (X [I]-min_x) * 500/(max_x-min_x) + 70; If (max_y = min_y) Y1 = 240; else Y1 = 480-(Y [I]-min_y) * 400/(max_y-min_y) + 40); If (mode = 0) circle (x1, Y1, 2); line (x0, y0, X1, Y1); X0 = x1; Y0 = Y1 ;} printf ("\ n"); printf ("% F", max_y ); printf ("\ n"); printf ("\ n "); printf ("% F", (max_y + min_y)/2 ); printf ("\ n"); printf ("\ n "); printf ("% F", min_y); printf ("\ n % F", min_x); printf (""); printf ("% F", (max_x + min_x) /2); printf (""); printf ("% F", max_x);} void plotpdf (float * X, int num, int part, int Mode) {int I, j; float max_x, min_x, round, deltax, up, down, sum; float XL [max_n], yl [max_n]; sum = 0; max_x = max (x, num); min_x = min (x, num); Round = max_x-min_x; deltax = round/part; XL [0] = min_x; for (I = 1; I <= part; I ++) {XL [I] = min_x + deltax * I; yl [I-1] = 0; up = XL [I]; down = XL [I-1]; for (j = 0; j <num; j ++) {If (X [J] <up) & (X [J]> = down) YL [I-1] = yl [I-1] + 1;} yl [I-1] = yl [I-1]/num/deltax ;} for (I = 0; I <part; I ++) sum = sum + yl [I]; plotxy (& XL, & YL, part, mode );} float max (float * X, int num) {int I; float Max; max = x [0]; for (I = 1; I <num; I ++) {If (X [I]> MAX) max = x [I];} return Max;} float min (float * X, int num) {int I; float min; min = x [0]; for (I = 1; I <num; I ++) {If (X [I] <min) min = x [I];} return min ;}

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.