random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

pi_check.h (2949B)


      1 /*
      2 Copyright 2010-2011, D. E. Shaw Research.
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are
      7 met:
      8 
      9 * Redistributions of source code must retain the above copyright
     10   notice, this list of conditions, and the following disclaimer.
     11 
     12 * Redistributions in binary form must reproduce the above copyright
     13   notice, this list of conditions, and the following disclaimer in the
     14   documentation and/or other materials provided with the distribution.
     15 
     16 * Neither the name of D. E. Shaw Research nor the names of its
     17   contributors may be used to endorse or promote products derived from
     18   this software without specific prior written permission.
     19 
     20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 */
     32 #ifndef PI_CHECK_H__
     33 #define PI_CHECK_H__ 1
     34 
     35 #include <stdio.h>
     36 
     37 const unsigned long NTRIES = 10000000UL;
     38 
     39 /* XX Cannot make this static, is included in some files that only use it
     40    under ifdef-conditionally, and we do not want to ifdef this to match. */
     41 int pi_check(unsigned long hits, unsigned long tries)
     42 {
     43     const double PI = 3.14159265358979323846;
     44     double ourpi, mean, var, delta, chisq;
     45     printf("%lu out of %lu darts thrown at a square board hit the inscribed circle\n",
     46            hits, tries);
     47     ourpi = 4.*hits/tries;
     48     printf("pi is approximately %.8g (diff = %.2g %%)\n", ourpi, (ourpi - PI)*100./PI);
     49     mean = tries*(PI/4.);
     50     var = tries * (PI/4.)*(1. - (PI/4.));
     51     delta = hits - mean;
     52     chisq = delta*delta/var;
     53     /*  Sigh.  Jump through hoops so we don't want to link with -lm for sqrt */
     54     if( chisq < 1. )
     55         printf("OK, # of hits is less than one 'sigma' away from expectation\n(chisquared = %.2g)\n", chisq);
     56     else if(chisq < 4.)
     57         printf("OK, # of hits is between one and two 'sigma' away from expectation\n(chisquared = %.2g)\n", chisq);
     58     else if(chisq < 9.)
     59         printf("Maybe OK, # of hits is between two and three 'sigma' away from expectation\n(chisquared = %.2g)\n", chisq);
     60     else {
     61         printf("May not be OK, # of hits is more than three 'sigma'.  Worth looking into.\n(chisquared = %.2g)\n", chisq);
     62 	return 1;
     63     }
     64     return 0;
     65 }
     66 
     67 #endif /* PI_CHECK_H__ */