ut_gsl.c (3918B)
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 <gsl/gsl_randist.h> 33 #include <stdio.h> 34 #include "Random123/philox.h" 35 #include "Random123/threefry.h" 36 #include "Random123/conventional/gsl_cbrng.h" 37 #include <assert.h> 38 39 /* Exercise the GSL_CBRNG macro */ 40 41 GSL_CBRNG(cbrng, threefry4x64); /* creates gsl_rng_cbrng */ 42 43 int main(int argc, char **argv){ 44 int i; 45 gsl_rng *r; 46 gsl_rng *rcopy; 47 unsigned long save, x; 48 unsigned long saved[5]; 49 double sum = 0.; 50 (void)argc; (void)argv; /* unused */ 51 52 r = gsl_rng_alloc(gsl_rng_cbrng); 53 assert (gsl_rng_min(r) == 0); 54 assert (gsl_rng_max(r) == 0xffffffffUL); // Not necessarily ~0UL 55 assert (gsl_rng_size(r) > 0); 56 57 printf("%s\nulongs from %s in initial state\n", argv[0], gsl_rng_name(r)); 58 for (i = 0; i < 5; i++) { 59 x = gsl_rng_get(r); 60 saved[i] = x; 61 printf("%d: 0x%lx\n", i, x); 62 assert(x != 0); 63 } 64 printf("uniforms from %s\n", gsl_rng_name(r)); 65 for (i = 0; i < 5; i++) { 66 double z = gsl_rng_uniform(r); 67 sum += z; 68 printf("%d: %.4g\n", i, z); 69 } 70 assert( sum < 0.9*5 && sum > 0.1*5 && (long)"sum must be reasonably close to 0.5*number of trials"); 71 save = gsl_rng_get(r); 72 73 gsl_rng_set(r, 0xdeadbeef); /* set a non-zero seed */ 74 printf("ulongs from %s after seed\n", gsl_rng_name(r)); 75 for (i = 0; i < 5; i++) { 76 x = gsl_rng_get(r); 77 printf("%d: 0x%lx\n", i, x); 78 assert(x != 0); 79 } 80 /* make a copy of the total state */ 81 rcopy = gsl_rng_alloc(gsl_rng_cbrng); 82 gsl_rng_memcpy(rcopy, r); 83 printf("uniforms from %s\n", gsl_rng_name(r)); 84 sum = 0.; 85 for (i = 0; i < 5; i++) { 86 double x = gsl_rng_uniform(r); 87 double y = gsl_rng_uniform(rcopy); 88 printf("%d: %.4g\n", i, x); 89 sum += x; 90 assert(x == y); 91 } 92 assert(gsl_rng_get(r) != save); 93 assert( sum < 0.9*5 && sum > 0.1*5 && (long)"sum must be reasonably close to 0.5*number of trials"); 94 95 /* gsl_rng_set(*, 0) is supposed to recover the default seed */ 96 gsl_rng_set(r, 0); 97 printf("ulongs from %s after restore to initial\n", gsl_rng_name(r)); 98 for (i = 0; i < 5; i++) { 99 x = gsl_rng_get(r); 100 assert( x == saved[i] ); 101 printf("%d: 0x%lx\n", i, x); 102 assert(x != 0); 103 } 104 printf("uniforms from %s\n", gsl_rng_name(r)); 105 for (i = 0; i < 5; i++) { 106 printf("%d: %.4g\n", i, gsl_rng_uniform(r)); 107 } 108 assert(gsl_rng_get(r) == save); 109 110 gsl_rng_free (r); 111 return 0; 112 }