 |
Changeset 3705
- Timestamp:
- 07/04/08 22:52:50
(2 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
| r3535 |
r3705 |
|
| 9 | 9 | author: Various |
|---|
| 10 | 10 | |
|---|
| | 11 | With gratitude to Dr Jurgen A Doornik. See his paper entitled |
|---|
| | 12 | "Conversion of high-period random numbers to floating point" |
|---|
| | 13 | |
|---|
| 11 | 14 | *******************************************************************************/ |
|---|
| 12 | 15 | |
|---|
| 13 | | module tango.math.Rand; |
|---|
| | 16 | module tango.math.Kiss; |
|---|
| 14 | 17 | |
|---|
| 15 | 18 | |
|---|
| … | … | |
| 25 | 28 | /****************************************************************************** |
|---|
| 26 | 29 | |
|---|
| 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 |
|---|
| 30 | 33 | generators to get a composite that will be fast, easy to code |
|---|
| 31 | 34 | have a very long period and pass all the tests put to it. |
|---|
| … | … | |
| 36 | 39 | z(n)=2*z(n-1)+z(n-2) +carry mod 2^32 |
|---|
| 37 | 40 | --- |
|---|
| | 41 | |
|---|
| 38 | 42 | The y's are a shift register sequence on 32bit binary vectors |
|---|
| 39 | 43 | 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 | |
|---|
| 44 | 52 | ******************************************************************************/ |
|---|
| 45 | 53 | |
|---|
| 46 | | struct RandomInteger |
|---|
| | 54 | struct Kiss |
|---|
| 47 | 55 | { |
|---|
| 48 | 56 | private uint kiss_k; |
|---|
| … | … | |
| 54 | 62 | private uint kiss_carry = 0; |
|---|
| 55 | 63 | |
|---|
| | 64 | private const double M_RAN_INVM32 = 2.32830643653869628906e-010, |
|---|
| | 65 | M_RAN_INVM52 = 2.22044604925031308085e-016; |
|---|
| | 66 | |
|---|
| 56 | 67 | /********************************************************************** |
|---|
| 57 | 68 | |
|---|
| … | … | |
| 60 | 71 | **********************************************************************/ |
|---|
| 61 | 72 | |
|---|
| 62 | | static RandomInteger opCall() |
|---|
| 63 | | { |
|---|
| 64 | | RandomInteger rand; |
|---|
| | 73 | static Kiss opCall() |
|---|
| | 74 | { |
|---|
| | 75 | Kiss rand; |
|---|
| 65 | 76 | rand.seed; |
|---|
| 66 | 77 | return rand; |
|---|
| … | … | |
| 152 | 163 | return next(max-min) + min; |
|---|
| 153 | 164 | } |
|---|
| | 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 | } |
|---|
| 154 | 190 | } |
|---|
| 155 | 191 | |
|---|
| 156 | 192 | |
|---|
| | 193 | |
|---|
| 157 | 194 | /****************************************************************************** |
|---|
| 158 | 195 | |
|---|
| 159 | | With much gratitude to Dr Jurgen A Doornik. See his paper entitled |
|---|
| 160 | | "Conversion of high-period random numbers to floating point" |
|---|
| 161 | 196 | |
|---|
| 162 | 197 | ******************************************************************************/ |
|---|
| 163 | 198 | |
|---|
| 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) |
|---|
| | 199 | debug (Kiss) |
|---|
| 239 | 200 | { |
|---|
| 240 | 201 | import tango.io.Stdout; |
|---|
| … | … | |
| 243 | 204 | void main() |
|---|
| 244 | 205 | { |
|---|
| 245 | | auto dbl = RandomDouble(); |
|---|
| | 206 | auto dbl = Kiss(); |
|---|
| 246 | 207 | auto count = 100_000_000; |
|---|
| 247 | 208 | StopWatch w; |
|---|
| r2889 |
r3705 |
|
| 9 | 9 | author: Various |
|---|
| 10 | 10 | |
|---|
| | 11 | Deprecated: Please use Kiss instead. We'll add a fully featured |
|---|
| | 12 | Random in a future release |
|---|
| | 13 | |
|---|
| 11 | 14 | *******************************************************************************/ |
|---|
| 12 | 15 | |
|---|
| … | … | |
| 25 | 28 | /****************************************************************************** |
|---|
| 26 | 29 | |
|---|
| 27 | | KISS (via George Marsaglia & Paul Hsieh) |
|---|
| | 30 | KISS (via George Marsaglia) |
|---|
| 28 | 31 | |
|---|
| 29 | 32 | the idea is to use simple, fast, individually promising |
|---|
| … | … | |
| 35 | 38 | y(n)=y(n-1)(I+L^13)(I+R^17)(I+L^5), |
|---|
| 36 | 39 | z(n)=2*z(n-1)+z(n-2) +carry mod 2^32 |
|---|
| 37 | | |
|---|
| | 40 | |
|---|
| 38 | 41 | The y's are a shift register sequence on 32bit binary vectors |
|---|
| 39 | 42 | period 2^32-1; The z's are a simple multiply-with-carry sequence |
|---|
| … | … | |
| 84 | 87 | **********************************************************************/ |
|---|
| 85 | 88 | |
|---|
| 86 | | this () |
|---|
| | 89 | deprecated this () |
|---|
| 87 | 90 | { |
|---|
| 88 | 91 | this.seed; |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic