# include # include # include # include # include int main ( void ); int i_uniform ( int b, int c, int *seed ); void test01 ( void ); void test02 ( void ); void timestamp ( void ); /**********************************************************************/ int main ( void ) /**********************************************************************/ /* Purpose: XDR_PRB calls sample problems for the XDR library. Modified: 22 February 2006 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "XDR_PRB\n" ); printf ( " Test the XDR external data representation library routines.\n" ); test01 ( ); test02 ( ); printf ( "\n" ); printf ( "XDR_PRB\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /**********************************************************************/ void test01 ( void ) /**********************************************************************/ /* Purpose: TEST01 tests XDR_INT for encoding data to a file. 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 = i_uniform ( 0, 100000, &seed ); printf ( " %d\n", xdr_i ); xdr_int ( &xdrs, &xdr_i ); } xdr_destroy ( &xdrs ); fclose ( file_pointer ); return; } /**********************************************************************/ void test02 ( void ) /**********************************************************************/ /* Purpose: TEST02 tests XDR_INT for decoding XDR data from a file. 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\n", xdr_i ); } xdr_destroy ( &xdrs ); fclose ( file_pointer ); return; } /******************************************************************************/ double d_uniform_01 ( int *seed ) /******************************************************************************/ /* Purpose: D_UNIFORM_01 returns a unit double precision pseudorandom number. Discussion: This routine implements the recursion seed = 16807 * seed mod ( 2**31 - 1 ) d_uniform_01 = seed / ( 2**31 - 1 ) The integer arithmetic never requires more than 32 bits, including a sign bit. If the initial seed is 12345, then the first three computations are Input Output D_UNIFORM_01 SEED SEED 12345 207482415 0.096616 207482415 1790989824 0.833995 1790989824 2035175616 0.947702 Modified: 11 August 2004 Author: John Burkardt Reference: Paul Bratley, Bennett Fox, L E Schrage, A Guide to Simulation, Springer Verlag, pages 201-202, 1983. Pierre L'Ecuyer, Random Number Generation, in Handbook of Simulation edited by Jerry Banks, Wiley Interscience, page 95, 1998. Bennett Fox, Algorithm 647: Implementation and Relative Efficiency of Quasirandom Sequence Generators, ACM Transactions on Mathematical Software, Volume 12, Number 4, pages 362-376, 1986. P A Lewis, A S Goodman, J M Miller, A Pseudo-Random Number Generator for the System/360, IBM Systems Journal, Volume 8, pages 136-143, 1969. Parameters: Input/output, int *SEED, the "seed" value. Normally, this value should not be 0. On output, SEED has been updated. Output, double D_UNIFORM_01, a new pseudorandom variate, strictly between 0 and 1. */ { int k; double r; k = *seed / 127773; *seed = 16807 * ( *seed - k * 127773 ) - k * 2836; if ( *seed < 0 ) { *seed = *seed + 2147483647; } /* Although SEED can be represented exactly as a 32 bit integer, it generally cannot be represented exactly as a 32 bit real number! */ r = ( double ) ( *seed ) * 4.656612875E-10; return r; } /******************************************************************************/ int i_uniform ( int b, int c, int *seed ) /******************************************************************************/ /* Purpose: I_UNIFORM returns a pseudorandom integer. Discussion: The pseudorandom number should be uniformly distributed between A and B. Modified: 26 February 2005 Author: John Burkardt Parameters: Input, int B, C, 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 I_UNIFORM, a number between A and B. */ { double d; int value; if ( b <= c ) { d = ( double ) ( b ) + ( double ) ( 1 + c - b ) * d_uniform_01 ( seed ); value = ( int ) ( d ); if ( value < b ) { value = b; } if ( c < value ) { value = c; } } else { d = ( double ) ( c ) + ( double ) ( 1 + b - c ) * d_uniform_01 ( seed ); value = ( int ) ( d ); if ( value < c ) { value = c; } if ( b < value ) { value = b; } } return value; } /*****************************************************************************/ void timestamp ( void ) /******************************************************************************/ /* Purpose: TIMESTAMP prints the current YMDHMS date as a time stamp. Example: 31 May 2001 09:45:54 AM Modified: 24 September 2003 Author: John Burkardt Parameters: None */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; size_t len; time_t now; now = time ( NULL ); tm = localtime ( &now ); len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }