| 1 | /* |
| 2 | * This file is part of libcxxsupport. |
| 3 | * |
| 4 | * libcxxsupport is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * libcxxsupport is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with libcxxsupport; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik |
| 21 | * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt |
| 22 | * (DLR). |
| 23 | */ |
| 24 | |
| 25 | /*! \file planck_rng.h |
| 26 | * This file contains the random number generator |
| 27 | * used by the Planck LevelS package. |
| 28 | * The generator is a C++ port of the xorshift generator xor128() described |
| 29 | * in Marsaglia, Journal of Statistical Software 2003, vol 8. |
| 30 | * It has a period of 2^128 - 1. |
| 31 | * |
| 32 | * Copyright (C) 2003, 2004, 2005 Max-Planck-Society |
| 33 | * \author Martin Reinecke |
| 34 | */ |
| 35 | |
| 36 | #ifndef PLANCK_RNG_H |
| 37 | #define PLANCK_RNG_Hcxx . . $ `cxxsupport/planck_rng.h:37:9`! |
| 38 | |
| 39 | #include <cmath> |
| 40 | #include "error_handling.h"cxx . . $ `<file>/cxxsupport/error_handling.h`/ |
| 41 | |
| 42 | /*! C++ port of the xorshift generator xor128() described in Marsaglia, |
| 43 | Journal of Statistical Software 2003, vol 8. |
| 44 | It has a period of 2^128 - 1. */ |
| 45 | class planck_rngcxx . . $ planck_rng# |
| 46 | { |
| 47 | private: |
| 48 | unsigned int xcxx . . $ planck_rng#x.,ycxx . . $ planck_rng#y.,zcxx . . $ planck_rng#z.,wcxx . . $ planck_rng#w.; |
| 49 | double smallcxx . . $ planck_rng#small., gsetcxx . . $ planck_rng#gset.; |
| 50 | bool emptycxx . . $ planck_rng#empty.; |
| 51 | |
| 52 | void twiddlecxx . . $ planck_rng#twiddle(db0e23f8c3d03d8b). (unsigned int &v) |
| 53 | { |
| 54 | for (int i=0; i<9; ++i) |
| 55 | { |
| 56 | v ^= v<<13; |
| 57 | v ^= v>>17; |
| 58 | v ^= v<<5; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void init_rngcxx . . $ planck_rng#init_rng(49f6e7a06ebc5aa8). () |
| 63 | { |
| 64 | // avoid zero seeds |
| 65 | if (xcxx . . $ planck_rng#x.==0) xcxx . . $ planck_rng#x. = 123456789; |
| 66 | if (ycxx . . $ planck_rng#y.==0) ycxx . . $ planck_rng#y. = 362436069; |
| 67 | if (zcxx . . $ planck_rng#z.==0) zcxx . . $ planck_rng#z. = 521288629; |
| 68 | if (wcxx . . $ planck_rng#w.==0) wcxx . . $ planck_rng#w. = 88675123; |
| 69 | |
| 70 | // shuffle the bits of the seeds |
| 71 | twiddlecxx . . $ planck_rng#twiddle(db0e23f8c3d03d8b).(xcxx . . $ planck_rng#x.); twiddlecxx . . $ planck_rng#twiddle(db0e23f8c3d03d8b).(ycxx . . $ planck_rng#y.); twiddlecxx . . $ planck_rng#twiddle(db0e23f8c3d03d8b).(zcxx . . $ planck_rng#z.); twiddlecxx . . $ planck_rng#twiddle(db0e23f8c3d03d8b).(wcxx . . $ planck_rng#w.); |
| 72 | |
| 73 | // burn in the RNG |
| 74 | for (int i=0; i<16; ++i) |
| 75 | int_rand_unicxx . . $ planck_rng#int_rand_uni(db52760e79bf9054).(); |
| 76 | } |
| 77 | |
| 78 | public: |
| 79 | /*! Initializes the generator with 0 to 4 seed values. */ |
| 80 | planck_rngcxx . . $ planck_rng#planck_rng(eda338842932bf2b). (unsigned int x1=123456789, unsigned int y1=362436069, |
| 81 | unsigned int z1=521288629, unsigned int w1=88675123) |
| 82 | : xcxx . . $ planck_rng#x.(x1), ycxx . . $ planck_rng#y.(y1), zcxx . . $ planck_rng#z.(z1), wcxx . . $ planck_rng#w.(w1), |
| 83 | smallcxx . . $ planck_rng#small.(1./(1.+double(0xFFFFFFFF))), gsetcxx . . $ planck_rng#gset.(0.), emptycxx . . $ planck_rng#empty.(true) |
| 84 | { |
| 85 | planck_assertcxx . . $ `cxxsupport/error_handling.h:79:9`! (sizeof(unsigned int)==4, "wrong integer size for RNG"); |
| 86 | init_rngcxx . . $ planck_rng#init_rng(49f6e7a06ebc5aa8).(); |
| 87 | } |
| 88 | |
| 89 | /*! Re-initializes the generator with 0 to 4 seed values. */ |
| 90 | void seedcxx . . $ planck_rng#seed(eda338842932bf2b). (unsigned int x1=123456789, unsigned int y1=362436069, |
| 91 | unsigned int z1=521288629, unsigned int w1=88675123) |
| 92 | { |
| 93 | xcxx . . $ planck_rng#x. = x1; ycxx . . $ planck_rng#y. = y1; zcxx . . $ planck_rng#z. = z1; wcxx . . $ planck_rng#w. = w1; |
| 94 | emptycxx . . $ planck_rng#empty. = true; |
| 95 | init_rngcxx . . $ planck_rng#init_rng(49f6e7a06ebc5aa8).(); |
| 96 | } |
| 97 | |
| 98 | /*! Returns uniformly distributed random integer numbers from the |
| 99 | interval [0;0xFFFFFFFF]. */ |
| 100 | unsigned int int_rand_unicxx . . $ planck_rng#int_rand_uni(db52760e79bf9054).() |
| 101 | { |
| 102 | unsigned int t = xcxx . . $ planck_rng#x.^(xcxx . . $ planck_rng#x.<<11); |
| 103 | xcxx . . $ planck_rng#x. = ycxx . . $ planck_rng#y.; |
| 104 | ycxx . . $ planck_rng#y. = zcxx . . $ planck_rng#z.; |
| 105 | zcxx . . $ planck_rng#z. = wcxx . . $ planck_rng#w.; |
| 106 | |
| 107 | return wcxx . . $ planck_rng#w.=(wcxx . . $ planck_rng#w.^(wcxx . . $ planck_rng#w.>>19))^(t^(t>>8)); |
| 108 | } |
| 109 | |
| 110 | //! Returns uniformly distributed random numbers from the interval [0;1[. |
| 111 | double rand_unicxx . . $ planck_rng#rand_uni(31fa7155c10fdd13).() |
| 112 | { |
| 113 | return smallcxx . . $ planck_rng#small.*int_rand_unicxx . . $ planck_rng#int_rand_uni(db52760e79bf9054).(); |
| 114 | } |
| 115 | |
| 116 | //! Returns random numbers with Gaussian distribution (mean=0, sigma=1). |
| 117 | /*! Uses rand_uni() internally. */ |
| 118 | double rand_gausscxx . . $ planck_rng#rand_gauss(31fa7155c10fdd13).() |
| 119 | { |
| 120 | using namespace std; |
| 121 | if (emptycxx . . $ planck_rng#empty.) |
| 122 | { |
| 123 | double v1,v2,rsq; |
| 124 | do |
| 125 | { |
| 126 | v1=2*rand_unicxx . . $ planck_rng#rand_uni(31fa7155c10fdd13).()-1.; |
| 127 | v2=2*rand_unicxx . . $ planck_rng#rand_uni(31fa7155c10fdd13).()-1.; |
| 128 | rsq=v1*v1+v2*v2; |
| 129 | } |
| 130 | while ((rsq>=1) || (rsq==0)); |
| 131 | double fac=sqrt(-2*log(rsq)/rsq); |
| 132 | gsetcxx . . $ planck_rng#gset.=v1*fac; |
| 133 | emptycxx . . $ planck_rng#empty.=false; |
| 134 | return v2*fac; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | emptycxx . . $ planck_rng#empty.=true; |
| 139 | return gsetcxx . . $ planck_rng#gset.; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | //! Returns exponentially distributed random numbers (mean=1, nonnegative) |
| 144 | /*! Uses rand_uni() internally. */ |
| 145 | double rand_expcxx . . $ planck_rng#rand_exp(31fa7155c10fdd13).() |
| 146 | { |
| 147 | using namespace std; |
| 148 | double val=rand_unicxx . . $ planck_rng#rand_uni(31fa7155c10fdd13).(); |
| 149 | if (val==0.) val=1.; |
| 150 | return -log(val); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | #endif |