-
Notifications
You must be signed in to change notification settings - Fork 2
/
ziggurat.cpp
667 lines (594 loc) · 13.7 KB
/
ziggurat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <ctime>
# include <stdint.h>
//
// My G++ compiler doesn't support this yet.
//
//# include <cstdint>
using namespace std;
# include "ziggurat.hpp"
//****************************************************************************80
uint32_t cong_seeded ( uint32_t &jcong )
//****************************************************************************80
//
// Purpose:
//
// CONG_SEEDED evaluates the CONG congruential random number generator.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Input/output, uint32_t &JCONG, the seed, which is updated
// on each call.
//
// Output, uint32_t CONG_SEEDED, the new value.
//
{
uint32_t value;
jcong = 69069 * ( jcong ) + 1234567;
value = jcong;
return value;
}
//****************************************************************************80
double cpu_time ( )
//****************************************************************************80
//
// Purpose:
//
// CPU_TIME returns the current reading on the CPU clock.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 December 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Output, double CPU_TIME, the current reading of the CPU clock, in seconds.
//
{
double value;
value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC;
return value;
}
//****************************************************************************80
uint32_t kiss_seeded ( uint32_t &jcong, uint32_t &jsr, uint32_t &w, uint32_t &z )
//****************************************************************************80
//
// Purpose:
//
// KISS_SEEDED evaluates the KISS random number generator.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Input/output, uint32_t &JCONG, uint32_t &JSR, uint32_t &W, uint32_t &Z,
// the seeds, which are updated on each call.
//
// Output, uint32_t KISS_SEEDED, the new value.
//
{
uint32_t value;
value = ( mwc_seeded ( w, z ) ^ cong_seeded ( jcong ) ) + shr3_seeded ( jsr );
return value;
}
//****************************************************************************80
uint32_t mwc_seeded ( uint32_t &w, uint32_t &z )
//****************************************************************************80
//
// Purpose:
//
// MWC_SEEDED evaluates the MWC multiply-with-carry random number generator.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Input/output, uint32_t &W, uint32_t &Z, the seeds, which are updated
// on each call.
//
// Output, uint32_t MWC_SEEDED, the new value.
//
{
uint32_t value;
z = 36969 * ( z & 65535 ) + ( z >> 16 );
w = 18000 * ( w & 65535 ) + ( w >> 16 );
value = ( z << 16 ) + w;
return value;
}
//****************************************************************************80
float r4_exp ( uint32_t &jsr, uint32_t ke[256], float fe[256], float we[256] )
//****************************************************************************80
//
// Purpose:
//
// R4_EXP returns an exponentially distributed single precision real value.
//
// Discussion:
//
// The underlying algorithm is the ziggurat method.
//
// Before the first call to this function, the user must call R4_EXP_SETUP
// to determine the values of KE, FE and WE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Input/output, uint32_t &JSR, the seed.
//
// Input, uint32_t KE[256], data computed by R4_EXP_SETUP.
//
// Input, float FE[256], WE[256], data computed by R4_EXP_SETUP.
//
// Output, float R4_EXP, an exponentially distributed random value.
//
{
uint32_t iz;
uint32_t jz;
float value;
float x;
jz = shr3_seeded ( jsr );
iz = ( jz & 255 );
if ( jz < ke[iz] )
{
value = ( float ) ( jz ) * we[iz];
}
else
{
for ( ; ; )
{
if ( iz == 0 )
{
value = 7.69711 - log ( r4_uni ( jsr ) );
break;
}
x = ( float ) ( jz ) * we[iz];
if ( fe[iz] + r4_uni ( jsr ) * ( fe[iz-1] - fe[iz] ) < exp ( - x ) )
{
value = x;
break;
}
jz = shr3_seeded ( jsr );
iz = ( jz & 255 );
if ( jz < ke[iz] )
{
value = ( float ) ( jz ) * we[iz];
break;
}
}
}
return value;
}
//****************************************************************************80
void r4_exp_setup ( uint32_t ke[256], float fe[256], float we[256] )
//****************************************************************************80
//
// Purpose:
//
// R4_EXP_SETUP sets data needed by R4_EXP.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Output, uint32_t KE[256], data needed by R4_EXP.
//
// Output, float FE[256], WE[256], data needed by R4_EXP.
//
{
double de = 7.697117470131487;
int i;
const double m2 = 2147483648.0;
double q;
double te = 7.697117470131487;
const double ve = 3.949659822581572E-03;
q = ve / exp ( - de );
ke[0] = ( uint32_t ) ( ( de / q ) * m2 );
ke[1] = 0;
we[0] = ( float ) ( q / m2 );
we[255] = ( float ) ( de / m2 );
fe[0] = 1.0;
fe[255] = ( float ) ( exp ( - de ) );
for ( i = 254; 1 <= i; i-- )
{
de = - log ( ve / de + exp ( - de ) );
ke[i+1] = ( uint32_t ) ( ( de / te ) * m2 );
te = de;
fe[i] = ( float ) ( exp ( - de ) );
we[i] = ( float ) ( de / m2 );
}
return;
}
//****************************************************************************80
float r4_nor ( uint32_t &jsr, uint32_t kn[128], float fn[128], float wn[128] )
//****************************************************************************80
//
// Purpose:
//
// R4_NOR returns a normally distributed single precision real value.
//
// Discussion:
//
// The value returned is generated from a distribution with mean 0 and
// variance 1.
//
// The underlying algorithm is the ziggurat method.
//
// Before the first call to this function, the user must call R4_NOR_SETUP
// to determine the values of KN, FN and WN.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Input/output, uint32_t &JSR, the seed.
//
// Input, uint32_t KN[128], data computed by R4_NOR_SETUP.
//
// Input, float FN[128], WN[128], data computed by R4_NOR_SETUP.
//
// Output, float R4_NOR, a normally distributed random value.
//
{
int hz;
uint32_t iz;
const float r = 3.442620;
float value;
float x;
float y;
hz = ( int ) shr3_seeded ( jsr );
iz = ( hz & 127 );
if ( fabs ( hz ) < kn[iz] )
{
value = ( float ) ( hz ) * wn[iz];
}
else
{
for ( ; ; )
{
if ( iz == 0 )
{
for ( ; ; )
{
x = - 0.2904764 * log ( r4_uni ( jsr ) );
y = - log ( r4_uni ( jsr ) );
if ( x * x <= y + y );
{
break;
}
}
if ( hz <= 0 )
{
value = - r - x;
}
else
{
value = + r + x;
}
break;
}
x = ( float ) ( hz ) * wn[iz];
if ( fn[iz] + r4_uni ( jsr ) * ( fn[iz-1] - fn[iz] )
< exp ( - 0.5 * x * x ) )
{
value = x;
break;
}
hz = ( int ) shr3_seeded ( jsr );
iz = ( hz & 127 );
if ( fabs ( hz ) < kn[iz] )
{
value = ( float ) ( hz ) * wn[iz];
break;
}
}
}
return value;
}
//****************************************************************************80
void r4_nor_setup ( uint32_t kn[128], float fn[128], float wn[128] )
//****************************************************************************80
//
// Purpose:
//
// R4_NOR_SETUP sets data needed by R4_NOR.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Output, uint32_t KN[128], data needed by R4_NOR.
//
// Output, float FN[128], WN[128], data needed by R4_NOR.
//
{
double dn = 3.442619855899;
int i;
const double m1 = 2147483648.0;
double q;
double tn = 3.442619855899;
const double vn = 9.91256303526217E-03;
q = vn / exp ( - 0.5 * dn * dn );
kn[0] = ( uint32_t ) ( ( dn / q ) * m1 );
kn[1] = 0;
wn[0] = ( float ) ( q / m1 );
wn[127] = ( float ) ( dn / m1 );
fn[0] = 1.0;
fn[127] = ( float ) ( exp ( - 0.5 * dn * dn ) );
for ( i = 126; 1 <= i; i-- )
{
dn = sqrt ( - 2.0 * log ( vn / dn + exp ( - 0.5 * dn * dn ) ) );
kn[i+1] = ( uint32_t ) ( ( dn / tn ) * m1 );
tn = dn;
fn[i] = ( float ) ( exp ( - 0.5 * dn * dn ) );
wn[i] = ( float ) ( dn / m1 );
}
return;
}
//****************************************************************************80
float r4_uni ( uint32_t &jsr )
//****************************************************************************80
//
// Purpose:
//
// R4_UNI returns a uniformly distributed real value.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 04 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Input/output, uint32_t &JSR, the seed.
//
// Output, float R4_UNI, a uniformly distributed random value in
// the range [0,1].
//
{
uint32_t jsr_input;
float value;
jsr_input = jsr;
jsr = ( jsr ^ ( jsr << 13 ) );
jsr = ( jsr ^ ( jsr >> 17 ) );
jsr = ( jsr ^ ( jsr << 5 ) );
value = fmod ( 0.5
+ ( float ) ( jsr_input + jsr ) / 65536.0 / 65536.0, 1.0 );
return value;
}
//****************************************************************************80
uint32_t shr3_seeded ( uint32_t &jsr )
//****************************************************************************80
//
// Purpose:
//
// SHR3_SEEDED evaluates the SHR3 generator for integers.
//
// Discussion:
//
// Thanks to Dirk Eddelbuettel for pointing out that this code needed to
// use the uint32_t data type in order to execute properly in 64 bit mode,
// 03 October 2013.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Marsaglia, Wai Wan Tsang,
// The Ziggurat Method for Generating Random Variables,
// Journal of Statistical Software,
// Volume 5, Number 8, October 2000, seven pages.
//
// Parameters:
//
// Input/output, uint32_t &JSR, the seed, which is updated
// on each call.
//
// Output, uint32_t SHR3_SEEDED, the new value.
//
{
uint32_t value;
value = jsr;
jsr = ( jsr ^ ( jsr << 13 ) );
jsr = ( jsr ^ ( jsr >> 17 ) );
jsr = ( jsr ^ ( jsr << 5 ) );
value = value + jsr;
return value;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 24 September 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct tm *tm;
size_t len;
time_t now;
now = time ( NULL );
tm = localtime ( &now );
len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}