| [658] | 1 | /*************************************************************************** | 
|---|
|  | 2 | * blitz/rand-uniform.h    Uniform class, which provides uniformly | 
|---|
|  | 3 | *                         distributed random numbers. | 
|---|
|  | 4 | * | 
|---|
|  | 5 | * $Id: rand-uniform.h,v 1.1.1.1 1999-11-26 16:37:04 ansari Exp $ | 
|---|
|  | 6 | * | 
|---|
|  | 7 | * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca> | 
|---|
|  | 8 | * | 
|---|
|  | 9 | * This program is free software; you can redistribute it and/or | 
|---|
|  | 10 | * modify it under the terms of the GNU General Public License | 
|---|
|  | 11 | * as published by the Free Software Foundation; either version 2 | 
|---|
|  | 12 | * of the License, or (at your option) any later version. | 
|---|
|  | 13 | * | 
|---|
|  | 14 | * This program is distributed in the hope that it will be useful, | 
|---|
|  | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 17 | * GNU General Public License for more details. | 
|---|
|  | 18 | * | 
|---|
|  | 19 | * Suggestions:          blitz-suggest@cybervision.com | 
|---|
|  | 20 | * Bugs:                 blitz-bugs@cybervision.com | 
|---|
|  | 21 | * | 
|---|
|  | 22 | * For more information, please see the Blitz++ Home Page: | 
|---|
|  | 23 | *    http://seurat.uwaterloo.ca/blitz/ | 
|---|
|  | 24 | * | 
|---|
|  | 25 | *************************************************************************** | 
|---|
|  | 26 | * | 
|---|
|  | 27 | * This random number generator is based on the LAPACK auxilliary | 
|---|
|  | 28 | * routine DLARAN by Jack Dongarra.  It's a multiplicative congruential | 
|---|
|  | 29 | * generator with modulus 2^48 and multiplier 33952834046453. | 
|---|
|  | 30 | * | 
|---|
|  | 31 | * See also: G. S. Fishman, Multiplicative congruential random number | 
|---|
|  | 32 | * generators with modulus 2^b: an exhaustive analysis for b=32 and | 
|---|
|  | 33 | * a partial analysis for b=48, Math. Comp. 189, pp 331-344, 1990. | 
|---|
|  | 34 | * | 
|---|
|  | 35 | * This routine requires 32-bit integers. | 
|---|
|  | 36 | * | 
|---|
|  | 37 | * The generated number lies in the open interval (low,high).  i.e. low and | 
|---|
|  | 38 | * high themselves will never be generated. | 
|---|
|  | 39 | * | 
|---|
|  | 40 | *************************************************************************** | 
|---|
|  | 41 | * | 
|---|
|  | 42 | * $Log: not supported by cvs2svn $ | 
|---|
|  | 43 | * Revision 1.1.1.1  1999/04/09  17:58:59  ansari | 
|---|
|  | 44 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99 | 
|---|
|  | 45 | * | 
|---|
|  | 46 | * Revision 1.4  1998/03/14 00:04:47  tveldhui | 
|---|
|  | 47 | * 0.2-alpha-05 | 
|---|
|  | 48 | * | 
|---|
|  | 49 | * Revision 1.3  1997/07/16 14:51:20  tveldhui | 
|---|
|  | 50 | * Update: Alpha release 0.2 (Arrays) | 
|---|
|  | 51 | * | 
|---|
|  | 52 | * Revision 1.2  1997/01/24 14:42:00  tveldhui | 
|---|
|  | 53 | * Periodic RCS update | 
|---|
|  | 54 | * | 
|---|
|  | 55 | */ | 
|---|
|  | 56 |  | 
|---|
|  | 57 | #ifndef BZ_RAND_UNIFORM_H | 
|---|
|  | 58 | #define BZ_RAND_UNIFORM_H | 
|---|
|  | 59 |  | 
|---|
|  | 60 | #ifndef BZ_RANDOM_H | 
|---|
|  | 61 | #include <blitz/random.h> | 
|---|
|  | 62 | #endif | 
|---|
|  | 63 |  | 
|---|
|  | 64 | BZ_NAMESPACE(blitz) | 
|---|
|  | 65 |  | 
|---|
|  | 66 | class Uniform { | 
|---|
|  | 67 |  | 
|---|
|  | 68 | public: | 
|---|
|  | 69 | typedef double T_numtype; | 
|---|
|  | 70 |  | 
|---|
|  | 71 | Uniform(double low = 0.0, double high = 1.0, double = 0.0) | 
|---|
|  | 72 | : low_(low), length_(high-low) | 
|---|
|  | 73 | { | 
|---|
|  | 74 | BZPRECONDITION(sizeof(int) >= 4);   // Need 32 bit integers! | 
|---|
|  | 75 |  | 
|---|
|  | 76 | seed[0] = 24;       // All seeds in the range [0,4095] | 
|---|
|  | 77 | seed[1] = 711; | 
|---|
|  | 78 | seed[2] = 3; | 
|---|
|  | 79 | seed[3] = 3721;     // The last seed must be odd | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 | void randomize() | 
|---|
|  | 83 | { | 
|---|
|  | 84 | BZ_NOT_IMPLEMENTED();            // NEEDS_WORK | 
|---|
|  | 85 |  | 
|---|
|  | 86 | BZPOSTCONDITION(seed[3] % 2 == 1); | 
|---|
|  | 87 | } | 
|---|
|  | 88 |  | 
|---|
|  | 89 | // I'm trying to avoid having a compiled | 
|---|
|  | 90 | // portion of the library, so this is inline until I | 
|---|
|  | 91 | // figure out a better way to do this or I change my mind. | 
|---|
|  | 92 | // -- TV | 
|---|
|  | 93 | // NEEDS_WORK | 
|---|
|  | 94 | double random() | 
|---|
|  | 95 | { | 
|---|
|  | 96 | BZPRECONDITION(seed[3] % 2 == 1); | 
|---|
|  | 97 |  | 
|---|
|  | 98 | int it0, it1, it2, it3; | 
|---|
|  | 99 | it3 = seed[3] * 2549; | 
|---|
|  | 100 | it2 = it3 / 4096; | 
|---|
|  | 101 | it3 -= it2 << 12; | 
|---|
|  | 102 | it2 += seed[2] * 2549 + seed[3] * 2508; | 
|---|
|  | 103 | it1 = it2 / 4096; | 
|---|
|  | 104 | it2 -= it1 << 12; | 
|---|
|  | 105 | it1 += seed[1] * 2549 + seed[2] * 2508 + seed[3] * 322; | 
|---|
|  | 106 | it0 = it1 / 4096; | 
|---|
|  | 107 | it1 -= it0 << 12; | 
|---|
|  | 108 | it0 += seed[0] * 2549 + seed[1] * 2508 + seed[2] * 322 + seed[3] * 494; | 
|---|
|  | 109 | it0 %= 4096; | 
|---|
|  | 110 | seed[0] = it0; | 
|---|
|  | 111 | seed[1] = it1; | 
|---|
|  | 112 | seed[2] = it2; | 
|---|
|  | 113 | seed[3] = it3; | 
|---|
|  | 114 |  | 
|---|
|  | 115 | const double z = 1 / 4096.; | 
|---|
|  | 116 | return low_ + length_ * (it0 + (it1 + (it2 + it3 * z) * z) * z) * z; | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
|  | 119 | operator double() | 
|---|
|  | 120 | { return random(); } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | private: | 
|---|
|  | 123 | double low_, length_; | 
|---|
|  | 124 |  | 
|---|
|  | 125 | int seed[4]; | 
|---|
|  | 126 | }; | 
|---|
|  | 127 |  | 
|---|
|  | 128 | BZ_NAMESPACE_END | 
|---|
|  | 129 |  | 
|---|
|  | 130 | #endif // BZ_RAND_UNIFORM_H | 
|---|
|  | 131 |  | 
|---|