pi_uniform.cpp (5233B)
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 #include <stdio.h> 33 #include <Random123/threefry.h> 34 #include <Random123/uniform.hpp> 35 36 /* Compute pi, using the u01 conversion with threefry2x64 and threefry2x32 */ 37 38 #include "pi_check.h" 39 #include "example_seeds.h" 40 41 using namespace r123; 42 43 template<typename Ftype, typename CBRNG> 44 void pi(typename CBRNG::key_type k); 45 46 int errs = 0; 47 int main(int, char **){ 48 uint64_t seed64 = example_seed_u64(EXAMPLE_SEED1_U64); // example user-settable seed 49 unsigned long hits = 0, tries = 0; 50 51 // First, we demonstrate how to compute pi 52 // using uneg11 to convert the integer output 53 // of threefry2x64 to a double in (-1, 1). 54 Threefry2x64::ctr_type c = {{0}}, r; 55 Threefry2x64::ukey_type uk = {{seed64}}; 56 Threefry2x64::key_type k = uk; 57 printf("%lu uniform doubles from threefry2x64\n", NTRIES); 58 while (tries < NTRIES) { 59 double x, y; 60 c.v[0]++; /* increment the counter */ 61 r = threefry2x64(c, k); 62 x = uneg11<double>(r.v[0]); 63 y = uneg11<double>(r.v[1]); 64 if( x*x + y*y < 1.0 ) 65 hits++; 66 tries++; 67 } 68 errs += pi_check(hits, tries); 69 70 // Extra credit: use some template hackery to exercise various 71 // combinations of float, double and long double, unit64_t and 72 // uint32_t and the conversion functions u01, uneg11 and ufixed01. 73 // This provides minimal testing of the conversion functions. 74 pi<float, Threefry2x64>(k); 75 pi<double, Threefry2x64>(k); 76 pi<long double, Threefry2x64>(k); 77 uint32_t seed32 = example_seed_u32(EXAMPLE_SEED9_U32); 78 79 Threefry2x32::ukey_type ukh = {{seed32}}; 80 Threefry2x32::key_type kh = ukh; 81 pi<float, Threefry2x32>(kh); 82 pi<double, Threefry2x32>(kh); 83 pi<long double, Threefry2x32>(kh); 84 85 return !!errs; 86 } 87 88 template<typename Ftype, typename CBRNG> 89 void pi(typename CBRNG::key_type k){ 90 unsigned long hits = 0, tries = 0; 91 CBRNG rng; 92 93 printf("Compute pi with uneg11:\n"); 94 typename CBRNG::ctr_type c = {{0}}, r; 95 hits = tries = 0; 96 while (tries < NTRIES) { 97 Ftype x, y; 98 c.v[0]++; /* increment the counter */ 99 r = rng(c, k); 100 // x and y in the entire square from (-1,-1) to (1,1) 101 x = uneg11<Ftype>(r.v[0]); 102 y = uneg11<Ftype>(r.v[1]); 103 if( x*x + y*y < 1.0 ) 104 hits++; 105 tries++; 106 } 107 errs += pi_check(hits, tries); 108 109 #if __cplusplus >= 201103L 110 printf("Compute pi with uneg11all (requires C++11):\n"); 111 hits = tries = 0; 112 while (tries < NTRIES) { 113 c.v[0]++; /* increment the counter */ 114 r = rng(c, k); 115 // x and y in the entire square from (-1,-1) to (1,1) 116 auto a = uneg11all<Ftype>(r); 117 if( a[0]*a[0] + a[1]*a[1] < 1.0 ) 118 hits++; 119 tries++; 120 } 121 errs += pi_check(hits, tries); 122 #endif 123 124 printf("Compute pi with u01:\n"); 125 hits = tries = 0; 126 while (tries < NTRIES) { 127 Ftype x, y; 128 c.v[0]++; /* increment the counter */ 129 r = rng(c, k); 130 // generate x and y in the first quadrant from (0,0) to (1,1) 131 x = u01<Ftype>(r.v[0]); 132 y = u01<Ftype>(r.v[1]); 133 if( x*x + y*y < 1.0 ) 134 hits++; 135 tries++; 136 } 137 errs += pi_check(hits, tries); 138 139 printf("Compute pi with u01fixedpt:\n"); 140 hits = tries = 0; 141 while (tries < NTRIES) { 142 Ftype x, y; 143 c.v[0]++; /* increment the counter */ 144 r = rng(c, k); 145 // generate x and y in the first quadrant from (0,0) to (1,1) 146 x = u01fixedpt<Ftype>(r.v[0]); 147 y = u01fixedpt<Ftype>(r.v[1]); 148 if( x*x + y*y < 1.0 ) 149 hits++; 150 tries++; 151 } 152 errs += pi_check(hits, tries); 153 } 154