Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3705

Show
Ignore:
Timestamp:
07/04/08 22:52:50 (5 months ago)
Author:
kris
Message:

deprecated Random, and renamed Rand to be Kiss instead. Folded floating point functions into Kiss

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/math/Kiss.d

    r3535 r3705  
    99        author:         Various 
    1010 
     11        With gratitude to Dr Jurgen A Doornik. See his paper entitled 
     12        "Conversion of high-period random numbers to floating point" 
     13         
    1114*******************************************************************************/ 
    1215 
    13 module tango.math.Rand
     16module tango.math.Kiss
    1417 
    1518 
     
    2528/****************************************************************************** 
    2629 
    27         KISS (via George Marsaglia & Paul Hsieh
    28  
    29         the idea is to use simple, fast, individually promising 
     30        KISS (from George Marsaglia
     31 
     32        The idea is to use simple, fast, individually promising 
    3033        generators to get a composite that will be fast, easy to code 
    3134        have a very long period and pass all the tests put to it. 
     
    3639                z(n)=2*z(n-1)+z(n-2) +carry mod 2^32 
    3740        --- 
     41 
    3842        The y's are a shift register sequence on 32bit binary vectors 
    3943        period 2^32-1; The z's are a simple multiply-with-carry sequence 
    40         with period 2^63+2^32-1. 
    41  
    42         The period of KISS is thus 2^32*(2^32-1)*(2^63+2^32-1) > 2^127 
    43  
     44        with period 2^63+2^32-1. The period of KISS is thus 
     45        --- 
     46                2^32*(2^32-1)*(2^63+2^32-1) > 2^127 
     47        --- 
     48 
     49        Note that this should be passed by reference, unless you really 
     50        intend to provide a local copy to a callee 
     51         
    4452******************************************************************************/ 
    4553 
    46 struct RandomInteger 
     54struct Kiss 
    4755{ 
    4856        private uint kiss_k; 
     
    5462        private uint kiss_carry = 0; 
    5563         
     64        private const double M_RAN_INVM32 = 2.32830643653869628906e-010, 
     65                             M_RAN_INVM52 = 2.22044604925031308085e-016; 
     66 
    5667        /********************************************************************** 
    5768 
     
    6071        **********************************************************************/ 
    6172 
    62         static RandomInteger opCall() 
    63         { 
    64                 RandomInteger rand; 
     73        static Kiss opCall() 
     74        { 
     75                Kiss rand; 
    6576                rand.seed; 
    6677                return rand; 
     
    152163                return next(max-min) + min; 
    153164        } 
     165         
     166        /********************************************************************** 
     167         
     168                Returns a value between 0 and 1, exclusive, using 32 bits 
     169                of precision (with thanks to Dr Jurgen A Doornik) 
     170 
     171        **********************************************************************/ 
     172 
     173        double next32 () 
     174        { 
     175                return (next * M_RAN_INVM32 + (0.5 + M_RAN_INVM32 / 2)); 
     176        } 
     177 
     178        /********************************************************************** 
     179 
     180                Returns a value between 0 and 1, exclusive, using 52 bits 
     181                of precision (with thanks to Dr Jurgen A Doornik) 
     182 
     183        **********************************************************************/ 
     184 
     185        double next52 () 
     186        { 
     187                return (next * M_RAN_INVM32 + (0.5 + M_RAN_INVM52 / 2) +  
     188                       (next & 0x000FFFFF) * M_RAN_INVM52); 
     189        } 
    154190} 
    155191 
    156192 
     193 
    157194/****************************************************************************** 
    158195 
    159         With much gratitude to Dr Jurgen A Doornik. See his paper entitled 
    160         "Conversion of high-period random numbers to floating point" 
    161196 
    162197******************************************************************************/ 
    163198 
    164 struct RandomDouble 
    165 
    166         RandomInteger source; 
    167  
    168         private const double M_RAN_INVM32 = 2.32830643653869628906e-010, 
    169                              M_RAN_INVM52 = 2.22044604925031308085e-016; 
    170  
    171         /********************************************************************** 
    172  
    173                 Creates and seeds a new generator with the current time 
    174  
    175         **********************************************************************/ 
    176  
    177         static RandomDouble opCall() 
    178         { 
    179                 RandomDouble rand; 
    180                 rand.source.seed; 
    181                 return rand; 
    182         } 
    183  
    184         /********************************************************************** 
    185  
    186                 Returns a value between 0 and 1, exclusive, using 32 bits 
    187                 of precision 
    188  
    189         **********************************************************************/ 
    190  
    191         double next32 () 
    192         { 
    193                 return next32 (source.next); 
    194         } 
    195  
    196         /********************************************************************** 
    197  
    198                 Returns a value between 0 and 1, exclusive, using 52 bits 
    199                 of precision 
    200  
    201         **********************************************************************/ 
    202  
    203         double next52 () 
    204         { 
    205                 return next52 (source.next, source.next); 
    206         } 
    207  
    208         /********************************************************************** 
    209          
    210                 Returns a value between 0 and 1, exclusive 
    211  
    212         **********************************************************************/ 
    213  
    214         double next32 (int random) 
    215         { 
    216                 return (random * M_RAN_INVM32 + (0.5 + M_RAN_INVM32 / 2)); 
    217         } 
    218  
    219         /********************************************************************** 
    220  
    221                 Returns a value between 0 and 1, exclusive 
    222  
    223         **********************************************************************/ 
    224  
    225         double next52 (int random1, int random2) 
    226         { 
    227                 return (random1 * M_RAN_INVM32 + (0.5 + M_RAN_INVM52 / 2) +  
    228                        (random2 & 0x000FFFFF) * M_RAN_INVM52); 
    229         } 
    230 
    231  
    232  
    233 /****************************************************************************** 
    234  
    235  
    236 ******************************************************************************/ 
    237  
    238 debug (Rand) 
     199debug (Kiss) 
    239200{ 
    240201        import tango.io.Stdout; 
     
    243204        void main() 
    244205        { 
    245                 auto dbl = RandomDouble(); 
     206                auto dbl = Kiss(); 
    246207                auto count = 100_000_000; 
    247208                StopWatch w; 
  • trunk/tango/math/Random.d

    r2889 r3705  
    99        author:         Various 
    1010 
     11        Deprecated:     Please use Kiss instead. We'll add a fully featured 
     12                        Random in a future release 
     13         
    1114*******************************************************************************/ 
    1215 
     
    2528/****************************************************************************** 
    2629 
    27         KISS (via George Marsaglia & Paul Hsieh
     30        KISS (via George Marsaglia
    2831 
    2932        the idea is to use simple, fast, individually promising 
     
    3538                y(n)=y(n-1)(I+L^13)(I+R^17)(I+L^5), 
    3639                z(n)=2*z(n-1)+z(n-2) +carry mod 2^32 
    37  
     40                 
    3841        The y's are a shift register sequence on 32bit binary vectors 
    3942        period 2^32-1; The z's are a simple multiply-with-carry sequence 
     
    8487        **********************************************************************/ 
    8588 
    86         this () 
     89        deprecated this () 
    8790        { 
    8891                this.seed;