# include # include # include # include # include # include int main ( ); int i4_uniform_ab ( int a, int b, int *seed ); void test01 ( ); void test02 ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: XDR_TEST calls sample problems for the XDR library. Licensing: This code is distributed under the GNU LGPL license. Modified: 22 February 2006 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "XDR_TEST\n" ); printf ( " C version\n" ); printf ( " Test the XDR external data representation library routines.\n" ); test01 ( ); test02 ( ); /* Terminate. */ printf ( "\n" ); printf ( "XDR_TEST\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void test01 ( ) /******************************************************************************/ /* Purpose: TEST01 tests XDR_INT for encoding data to a file. Licensing: This code is distributed under the GNU LGPL license. Modified: 23 February 2006 Author: John Burkardt */ { char *file_name = "test01.xdr"; FILE *file_pointer; int i; int seed; int xdr_i; XDR xdrs; printf ( "\n" ); printf ( "TEST01\n" ); printf ( " XDR_INT can be used to encode a four byte integer\n" ); printf ( " and write it to a file.\n" ); printf ( "\n" ); printf ( " I\n" ); printf ( "\n" ); file_pointer = fopen ( file_name, "w" ); xdrstdio_create ( &xdrs, file_pointer, XDR_ENCODE ); seed = 123456789; for ( i = 1; i <= 10; i++ ) { xdr_i = i4_uniform_ab ( 0, 100000, &seed ); printf ( " %d %d\n", i, xdr_i ); xdr_int ( &xdrs, &xdr_i ); } xdr_destroy ( &xdrs ); fclose ( file_pointer ); return; } /******************************************************************************/ void test02 ( ) /******************************************************************************/ /* Purpose: TEST02 tests XDR_INT for decoding XDR data from a file. Licensing: This code is distributed under the GNU LGPL license. Modified: 23 February 2006 Author: John Burkardt */ { char *file_name = "test01.xdr"; FILE *file_pointer; int i; int xdr_i; XDR xdrs; printf ( "\n" ); printf ( "TEST02\n" ); printf ( " XDR_INT can be used to read an encoded\n" ); printf ( " four byte integer from a file, and decode it.\n" ); printf ( "\n" ); printf ( " I\n" ); printf ( "\n" ); file_pointer = fopen ( file_name, "r" ); xdrstdio_create ( &xdrs, file_pointer, XDR_DECODE ); for ( i = 1; i <= 10; i++ ) { xdr_int ( &xdrs, &xdr_i ); printf ( " %d %d\n", i, xdr_i ); } xdr_destroy ( &xdrs ); fclose ( file_pointer ); return; } /******************************************************************************/ int i4_uniform_ab ( int a, int b, int *seed ) /******************************************************************************/ /* Purpose: I4_UNIFORM_AB returns a scaled pseudorandom I4 between A and B. Discussion: The pseudorandom number should be uniformly distributed between A and B. Licensing: This code is distributed under the GNU LGPL license. Modified: 24 May 2012 Author: John Burkardt Reference: Paul Bratley, Bennett Fox, Linus Schrage, A Guide to Simulation, Second Edition, Springer, 1987, ISBN: 0387964673, LC: QA76.9.C65.B73. Bennett Fox, Algorithm 647: Implementation and Relative Efficiency of Quasirandom Sequence Generators, ACM Transactions on Mathematical Software, Volume 12, Number 4, December 1986, pages 362-376. Pierre L'Ecuyer, Random Number Generation, in Handbook of Simulation, edited by Jerry Banks, Wiley, 1998, ISBN: 0471134031, LC: T57.62.H37. Peter Lewis, Allen Goodman, James Miller, A Pseudo-Random Number Generator for the System/360, IBM Systems Journal, Volume 8, Number 2, 1969, pages 136-143. Parameters: Input, int A, B, the limits of the interval. Input/output, int *SEED, the "seed" value, which should NOT be 0. On output, SEED has been updated. Output, int I4_UNIFORM_AB, a number between A and B. */ { int c; const int i4_huge = 2147483647; int k; float r; int value; if ( *seed == 0 ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "I4_UNIFORM_AB - Fatal error!\n" ); fprintf ( stderr, " Input value of SEED = 0.\n" ); exit ( 1 ); } /* Guaranteee A <= B. */ if ( b < a ) { c = a; a = b; b = c; } k = *seed / 127773; *seed = 16807 * ( *seed - k * 127773 ) - k * 2836; if ( *seed < 0 ) { *seed = *seed + i4_huge; } r = ( float ) ( *seed ) * 4.656612875E-10; /* Scale R to lie between A-0.5 and B+0.5. */ r = ( 1.0 - r ) * ( ( float ) ( a ) - 0.5 ) + r * ( ( float ) ( b ) + 0.5 ); /* Round R to the nearest integer. */ value = round ( r ); /* Guarantee that A <= VALUE <= B. */ if ( value < a ) { value = a; } if ( b < value ) { value = b; } return value; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: TIMESTAMP prints the current YMDHMS date as a time stamp. Example: 31 May 2001 09:45:54 AM Licensing: This code is distributed under the GNU LGPL license. Modified: 24 September 2003 Author: John Burkardt Parameters: None */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }