 |
Changeset 3974
- Timestamp:
- 10/05/08 15:16:48
(2 months ago)
- Author:
- kris
- Message:
method renaming
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3759 |
r3974 |
|
| 56 | 56 | struct Kiss |
|---|
| 57 | 57 | { |
|---|
| | 58 | public alias natural toInt; |
|---|
| | 59 | public alias decimal toReal; |
|---|
| | 60 | |
|---|
| 58 | 61 | private uint kiss_k; |
|---|
| 59 | 62 | private uint kiss_m; |
|---|
| … | … | |
| 137 | 140 | **********************************************************************/ |
|---|
| 138 | 141 | |
|---|
| 139 | | uint toInt () |
|---|
| | 142 | uint natural () |
|---|
| 140 | 143 | { |
|---|
| 141 | 144 | kiss_x = kiss_x * 69069 + 1; |
|---|
| … | … | |
| 160 | 163 | **********************************************************************/ |
|---|
| 161 | 164 | |
|---|
| 162 | | uint toInt (uint max) |
|---|
| 163 | | { |
|---|
| 164 | | return toInt() % max; |
|---|
| | 165 | uint natural (uint max) |
|---|
| | 166 | { |
|---|
| | 167 | return natural % max; |
|---|
| 165 | 168 | } |
|---|
| 166 | 169 | |
|---|
| … | … | |
| 174 | 177 | **********************************************************************/ |
|---|
| 175 | 178 | |
|---|
| 176 | | uint toInt (uint min, uint max) |
|---|
| 177 | | { |
|---|
| 178 | | return toInt(max-min) + min; |
|---|
| 179 | | } |
|---|
| 180 | | |
|---|
| 181 | | /********************************************************************** |
|---|
| 182 | | |
|---|
| 183 | | Returns a value between 0 and 1, exclusive, using 32 bits |
|---|
| | 179 | uint natural (uint min, uint max) |
|---|
| | 180 | { |
|---|
| | 181 | return (natural % (max-min)) + min; |
|---|
| | 182 | } |
|---|
| | 183 | |
|---|
| | 184 | /********************************************************************** |
|---|
| | 185 | |
|---|
| | 186 | Returns a value in the range [0, 1) using 32 bits |
|---|
| 184 | 187 | of precision (with thanks to Dr Jurgen A Doornik) |
|---|
| 185 | 188 | |
|---|
| 186 | 189 | **********************************************************************/ |
|---|
| 187 | 190 | |
|---|
| 188 | | double toReal () |
|---|
| 189 | | { |
|---|
| 190 | | return ((cast(int) toInt) * M_RAN_INVM32 + (0.5 + M_RAN_INVM32 / 2)); |
|---|
| 191 | | } |
|---|
| 192 | | |
|---|
| 193 | | /********************************************************************** |
|---|
| 194 | | |
|---|
| 195 | | Returns a value between 0 and 1, exclusive, using 52 bits |
|---|
| | 191 | double decimal () |
|---|
| | 192 | { |
|---|
| | 193 | return ((cast(int) natural) * M_RAN_INVM32 + (0.5 + M_RAN_INVM32 / 2)); |
|---|
| | 194 | } |
|---|
| | 195 | |
|---|
| | 196 | /********************************************************************** |
|---|
| | 197 | |
|---|
| | 198 | Returns a value in the range [0, 1) using 52 bits |
|---|
| 196 | 199 | of precision (with thanks to Dr Jurgen A Doornik) |
|---|
| 197 | 200 | |
|---|
| 198 | 201 | **********************************************************************/ |
|---|
| 199 | 202 | |
|---|
| 200 | | double toRealEx () |
|---|
| 201 | | { |
|---|
| 202 | | return ((cast(int) toInt) * M_RAN_INVM32 + (0.5 + M_RAN_INVM52 / 2) + |
|---|
| 203 | | ((cast(int) toInt) & 0x000FFFFF) * M_RAN_INVM52); |
|---|
| | 203 | double decimalEx () |
|---|
| | 204 | { |
|---|
| | 205 | return ((cast(int) natural) * M_RAN_INVM32 + (0.5 + M_RAN_INVM52 / 2) + |
|---|
| | 206 | ((cast(int) natural) & 0x000FFFFF) * M_RAN_INVM52); |
|---|
| 204 | 207 | } |
|---|
| 205 | 208 | } |
|---|
| … | … | |
| 226 | 229 | double v1; |
|---|
| 227 | 230 | for (int i=count; --i;) |
|---|
| 228 | | v1 = dbl.toReal; |
|---|
| 229 | | Stdout.formatln ("{} toReal, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| | 231 | v1 = dbl.decimal; |
|---|
| | 232 | Stdout.formatln ("{} decimal, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| 230 | 233 | |
|---|
| 231 | 234 | w.start; |
|---|
| 232 | 235 | for (int i=count; --i;) |
|---|
| 233 | | v1 = dbl.toRealEx; |
|---|
| 234 | | Stdout.formatln ("{} toRealEx, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| | 236 | v1 = dbl.decimalEx; |
|---|
| | 237 | Stdout.formatln ("{} decimalEx, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| 235 | 238 | |
|---|
| 236 | 239 | for (int i=count; --i;) |
|---|
| 237 | 240 | { |
|---|
| 238 | | auto v = dbl.toReal; |
|---|
| | 241 | auto v = dbl.decimal; |
|---|
| 239 | 242 | if (v <= 0.0 || v >= 1.0) |
|---|
| 240 | 243 | { |
|---|
| 241 | | Stdout.formatln ("toReal {:f10}", v); |
|---|
| | 244 | Stdout.formatln ("decimal {:f10}", v); |
|---|
| 242 | 245 | break; |
|---|
| 243 | 246 | } |
|---|
| 244 | | v = dbl.toRealEx; |
|---|
| | 247 | v = dbl.decimalEx; |
|---|
| 245 | 248 | if (v <= 0.0 || v >= 1.0) |
|---|
| 246 | 249 | { |
|---|
| 247 | | Stdout.formatln ("toRealEx {:f10}", v); |
|---|
| | 250 | Stdout.formatln ("decimalEx {:f10}", v); |
|---|
| 248 | 251 | break; |
|---|
| 249 | 252 | } |
|---|
| r3965 |
r3974 |
|
| 45 | 45 | struct Twister |
|---|
| 46 | 46 | { |
|---|
| | 47 | public alias natural toInt; |
|---|
| | 48 | public alias decimal toReal; |
|---|
| | 49 | |
|---|
| 47 | 50 | private enum : uint // Period parameters |
|---|
| 48 | 51 | { |
|---|
| … | … | |
| 90 | 93 | **********************************************************************/ |
|---|
| 91 | 94 | |
|---|
| 92 | | uint toInt (uint max) |
|---|
| 93 | | { |
|---|
| 94 | | return toInt % max; |
|---|
| | 95 | uint natural (uint max) |
|---|
| | 96 | { |
|---|
| | 97 | return natural % max; |
|---|
| 95 | 98 | } |
|---|
| 96 | 99 | |
|---|
| … | … | |
| 101 | 104 | **********************************************************************/ |
|---|
| 102 | 105 | |
|---|
| 103 | | uint toInt (uint min, uint max) |
|---|
| 104 | | { |
|---|
| 105 | | return (toInt % (max-min)) + min; |
|---|
| | 106 | uint natural (uint min, uint max) |
|---|
| | 107 | { |
|---|
| | 108 | return (natural % (max-min)) + min; |
|---|
| 106 | 109 | } |
|---|
| 107 | 110 | |
|---|
| … | … | |
| 112 | 115 | **********************************************************************/ |
|---|
| 113 | 116 | |
|---|
| 114 | | uint toInt (bool pAddEntropy = false) |
|---|
| | 117 | uint natural (bool pAddEntropy = false) |
|---|
| 115 | 118 | { |
|---|
| 116 | 119 | uint y; |
|---|
| … | … | |
| 153 | 156 | /********************************************************************** |
|---|
| 154 | 157 | |
|---|
| 155 | | generates a random number on [0,1]-real-interval |
|---|
| 156 | | |
|---|
| 157 | | **********************************************************************/ |
|---|
| 158 | | |
|---|
| 159 | | double toReal () |
|---|
| 160 | | { |
|---|
| 161 | | return toInt*(1.0/cast(double)uint.max); |
|---|
| 162 | | } |
|---|
| 163 | | |
|---|
| 164 | | /********************************************************************** |
|---|
| 165 | | |
|---|
| 166 | | generates a random number on [0,1)-real-interval |
|---|
| 167 | | |
|---|
| 168 | | **********************************************************************/ |
|---|
| 169 | | |
|---|
| 170 | | double toReal1 () |
|---|
| 171 | | { |
|---|
| 172 | | return toInt*(1.0/(cast(double)uint.max+1.0)); |
|---|
| 173 | | } |
|---|
| 174 | | |
|---|
| 175 | | /********************************************************************** |
|---|
| 176 | | |
|---|
| 177 | | generates a random number on (0,1)-real-interval |
|---|
| 178 | | |
|---|
| 179 | | **********************************************************************/ |
|---|
| 180 | | |
|---|
| 181 | | double toReal2 () |
|---|
| 182 | | { |
|---|
| 183 | | return ((cast(double)toInt) + 0.5)*(1.0/(cast(double)uint.max+1.0)); |
|---|
| | 158 | generates a random number on [0,1] interval |
|---|
| | 159 | |
|---|
| | 160 | **********************************************************************/ |
|---|
| | 161 | |
|---|
| | 162 | double inclusive () |
|---|
| | 163 | { |
|---|
| | 164 | return natural*(1.0/cast(double)uint.max); |
|---|
| | 165 | } |
|---|
| | 166 | |
|---|
| | 167 | /********************************************************************** |
|---|
| | 168 | |
|---|
| | 169 | generates a random number on (0,1) interval |
|---|
| | 170 | |
|---|
| | 171 | **********************************************************************/ |
|---|
| | 172 | |
|---|
| | 173 | double exclusive () |
|---|
| | 174 | { |
|---|
| | 175 | return ((cast(double)natural) + 0.5)*(1.0/(cast(double)uint.max+1.0)); |
|---|
| | 176 | } |
|---|
| | 177 | |
|---|
| | 178 | /********************************************************************** |
|---|
| | 179 | |
|---|
| | 180 | generates a random number on [0,1) interval |
|---|
| | 181 | |
|---|
| | 182 | **********************************************************************/ |
|---|
| | 183 | |
|---|
| | 184 | double decimal () |
|---|
| | 185 | { |
|---|
| | 186 | return natural*(1.0/(cast(double)uint.max+1.0)); |
|---|
| 184 | 187 | } |
|---|
| 185 | 188 | |
|---|
| … | … | |
| 190 | 193 | **********************************************************************/ |
|---|
| 191 | 194 | |
|---|
| 192 | | double toRealEx () |
|---|
| 193 | | { |
|---|
| 194 | | uint a=toInt >> 5, b=toInt >> 6; |
|---|
| 195 | | return(a*67108864.0+b)*(1.0/9007199254740992.0); |
|---|
| | 195 | double decimalEx () |
|---|
| | 196 | { |
|---|
| | 197 | uint a = natural >> 5, b = natural >> 6; |
|---|
| | 198 | return(a * 67108864.0 + b) * (1.0 / 9007199254740992.0); |
|---|
| 196 | 199 | } |
|---|
| 197 | 200 | |
|---|
| … | … | |
| 276 | 279 | double v1; |
|---|
| 277 | 280 | for (int i=count; --i;) |
|---|
| 278 | | v1 = dbl.toReal; |
|---|
| 279 | | Stdout.formatln ("{} toReal, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| | 281 | v1 = dbl.decimal; |
|---|
| | 282 | Stdout.formatln ("{} decimal, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| 280 | 283 | |
|---|
| 281 | 284 | w.start; |
|---|
| 282 | 285 | for (int i=count; --i;) |
|---|
| 283 | | v1 = dbl.toRealEx; |
|---|
| 284 | | Stdout.formatln ("{} toRealEx, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| | 286 | v1 = dbl.decimalEx; |
|---|
| | 287 | Stdout.formatln ("{} decimalEx, {}/s, {:f10}", count, count/w.stop, v1); |
|---|
| 285 | 288 | |
|---|
| 286 | 289 | for (int i=count; --i;) |
|---|
| 287 | 290 | { |
|---|
| 288 | | auto v = dbl.toReal; |
|---|
| | 291 | auto v = dbl.decimal; |
|---|
| 289 | 292 | if (v <= 0.0 || v >= 1.0) |
|---|
| 290 | 293 | { |
|---|
| 291 | | Stdout.formatln ("toReal {:f10}", v); |
|---|
| | 294 | Stdout.formatln ("decimal {:f10}", v); |
|---|
| 292 | 295 | break; |
|---|
| 293 | 296 | } |
|---|
| 294 | | v = dbl.toRealEx; |
|---|
| | 297 | v = dbl.decimalEx; |
|---|
| 295 | 298 | if (v <= 0.0 || v >= 1.0) |
|---|
| 296 | 299 | { |
|---|
| 297 | | Stdout.formatln ("toRealEx {:f10}", v); |
|---|
| | 300 | Stdout.formatln ("decimalEx {:f10}", v); |
|---|
| 298 | 301 | break; |
|---|
| 299 | 302 | } |
|---|
Download in other formats:
|
 |