-
Notifications
You must be signed in to change notification settings - Fork 0
/
randistrs.c
1175 lines (1106 loc) · 40.1 KB
/
randistrs.c
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef lint
static char Rcs_Id[] =
"$Id: randistrs.c,v 1.10 2010-12-11 00:28:19+13 geoff Exp $";
#endif
/*
* C library functions for generating various random distributions
* using the Mersenne Twist PRNG. See the header file for full
* documentation.
*
* These functions were written by Geoff Kuenning, Claremont, CA.
*
* Unless otherwise specified, these algorithms are taken from Averill
* M. Law and W. David Kelton, "Simulation Modeling and Analysis",
* McGraw-Hill, 1991.
*
* IMPORTANT NOTE: By default, this code is reentrant. If you are
* certain you don't need reentrancy, you can get a bit more speed by
* defining MT_CACHING.
*
* Copyright 2001, 2002, 2010, Geoffrey H. Kuenning, Claremont, CA.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All modifications to the source code must be clearly marked as
* such. Binary redistributions based on modified source code
* must be clearly marked as modified versions in the documentation
* and/or other materials provided with the distribution.
* 4. The name of Geoff Kuenning may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Log: randistrs.c,v $
* Revision 1.10 2010-12-11 00:28:19+13 geoff
* Rewrite the empirical-distribution interface to run in O(1) time and
* to provide a continuous approximation to empirical distributions.
*
* Revision 1.9 2010-06-24 20:53:59+12 geoff
* Switch to using types from stdint.h. Make reentrancy the default.
*
* Revision 1.8 2008-07-25 16:34:01-07 geoff
* Fix notation for intervals in commentary.
*
* Revision 1.7 2005/05/17 21:40:10 geoff
* Fix a bug that caused rds_iuniform to generate off-by-one values if the
* lower bound was negative.
*
* Revision 1.6 2002/10/30 00:50:44 geoff
* Add a (BSD-style) license. Fix all places where logs are taken so
* that there is no risk of unintentionally taking the log of zero. This
* is a very low-probability occurrence, but it's better to have robust
* code.
*
* Revision 1.5 2001/06/20 09:07:57 geoff
* Fix a place where long long wasn't conditionalized.
*
* Revision 1.4 2001/06/19 00:41:17 geoff
* Add the "l" versions of all functions. Add the MT_NO_CACHING option.
*
* Revision 1.3 2001/06/18 10:09:24 geoff
* Add the iuniform functions to generate unbiased uniformly distributed
* integers.
*
* Revision 1.2 2001/04/10 09:11:38 geoff
* Make sure the Erlang distribution has a p of 1 or more. Fix a serious
* bug in the Erlang calculation (the value returned was completely
* wrong).
*
* Revision 1.1 2001/04/09 08:39:54 geoff
* Initial revision
*
*/
#include "mtwist.h"
#include "randistrs.h"
#include <math.h>
#include <stdlib.h>
/*
* Table of contents:
*/
int32_t rds_iuniform(mt_state * state, int32_t lower,
int32_t upper);
/* (Integer) uniform distribution */
#ifdef INT64_MAX
int64_t rds_liuniform(mt_state * state, int64_t lower,
int64_t upper);
/* (Integer) uniform distribution */
#endif /* INT64_MAX */
double rds_uniform(mt_state * state,
double lower, double upper);
/* (Floating) uniform distribution */
double rds_luniform(mt_state * state,
double lower, double upper);
/* (Floating) uniform distribution */
double rds_exponential(mt_state * state, double mean);
/* Exponential distribution */
double rds_lexponential(mt_state * state, double mean);
/* Exponential distribution */
double rds_erlang(mt_state * state, int p, double mean);
/* p-Erlang distribution */
double rds_lerlang(mt_state * state, int p, double mean);
/* p-Erlang distribution */
double rds_weibull(mt_state * state,
double shape, double scale);
/* Weibull distribution */
double rds_lweibull(mt_state * state,
double shape, double scale);
/* Weibull distribution */
double rds_normal(mt_state * state,
double mean, double sigma);
/* Normal distribution */
double rds_lnormal(mt_state * state,
double mean, double sigma);
/* Normal distribution */
double rds_lognormal(mt_state * state,
double shape, double scale);
/* Lognormal distribution */
double rds_llognormal(mt_state * state,
double shape, double scale);
/* Lognormal distribution */
double rds_triangular(mt_state * state,
double lower, double upper, double mode);
/* Triangular distribution */
double rds_ltriangular(mt_state * state,
double lower, double upper, double mode);
/* Triangular distribution */
size_t rds_int_empirical(mt_state* state,
rd_empirical_control* control);
/* Discrete integer empirical distr. */
double rds_double_empirical(mt_state* state,
rd_empirical_control* control);
/* Discrete float empirical distr. */
double rds_continuous_empirical(mt_state* state,
rd_empirical_control* control);
/* Continuous empirical distribution */
int32_t rd_iuniform(int32_t lower, int32_t upper);
/* (Integer) uniform distribution */
#ifdef INT64_MAX
int64_t rd_liuniform(int64_t lower, int64_t upper);
/* (Integer) uniform distribution */
#endif /* INT64_MAX */
double rd_uniform(double lower, double upper);
/* (Floating) uniform distribution */
double rd_luniform(double lower, double upper);
/* (Floating) uniform distribution */
double rd_exponential(double mean);
/* Exponential distribution */
double rd_lexponential(double mean);
/* Exponential distribution */
double rd_erlang(int p, double mean);
/* p-Erlang distribution */
double rd_lerlang(int p, double mean);
/* p-Erlang distribution */
double rd_weibull(double shape, double scale);
/* Weibull distribution */
double rd_lweibull(double shape, double scale);
/* Weibull distribution */
double rd_normal(double mean, double sigma);
/* Normal distribution */
double rd_lnormal(double mean, double sigma);
/* Normal distribution */
double rd_lognormal(double shape, double scale);
/* Lognormal distribution */
double rd_llognormal(double shape, double scale);
/* Lognormal distribution */
double rd_triangular(double lower, double upper, double mode);
/* Triangular distribution */
double rd_ltriangular(double lower, double upper, double mode);
/* Triangular distribution */
rd_empirical_control* rd_empirical_setup(size_t n_probs,
double* probs, double* values);
/* Set up empirical distribution */
void rd_empirical_free(rd_empirical_control* control);
/* Free empirical control structure */
size_t rd_int_empirical(rd_empirical_control* control);
/* Discrete integer empirical distr. */
double rd_double_empirical(rd_empirical_control* control);
/* Discrete float empirical distr. */
double rd_continuous_empirical(rd_empirical_control* control);
/* Continuous empirical distribution */
/*
* The Mersenne Twist PRNG makes it default state available as an
* external variable. This feature is undocumented, but is useful to
* use because it allows us to avoid implementing every function
* twice. (In fact, the feature was added to enable this file to be
* written. It would be better to write in C++, where I could control
* the access to the state.)
*/
extern mt_state mt_default_state;
/*
* Threshold below which it is OK for uniform integer distributions to make
* use of the double-precision code as a crutch. For ranges below
* this value, a double-precision random value is generated and then
* mapped to the given range. For a lower bound of zero, this is
* equivalent to mapping a 32-bit integer into the range by using the
* following formula:
*
* final = upper * mt_lrand() / (1 << 32);
*
* That formula can't be computed using integer arithmetic, since the
* multiplication must precede the division and would cause overflow.
* Double-precision calculations solve that problem. However the
* formula will also produce biased results unless the range ("upper")
* is exactly a power of 2. To see this, suppose mt_lrand produced
* values from 0 to 7 (i.e., 8 values), and we asked for numbers in
* the range [0, 7). The 8 values uniformly generated by mt_lrand
* would be mapped into the 7 output values. Clearly, one output
* value (in this case, 4) would occur twice as often as the others
*
* The amount of bias introduced by this approximation depends on the
* relative sizes of the requested range and the range of values
* produced by mt_lrand. If the ranges are almost equal, some values
* will occur almost twice as often as they should. At the other
* extreme, consider a requested range of 3 values (0 to 2,
* inclusive). If the PRNG cycles through all 2^32 possible values,
* two of the output values will be generated 1431655765 times and the
* third will appear 1431655766 times. Clearly, the bias here is
* within the expected limits of randomness.
*
* The exact amount of bias depends on the relative size of the range
* compared to the width of the PRNG output. In general, for an
* output range of r, no value will appear more than r/(2^32) extra
* times using the simple integer algorithm.
*
* The threshold given below will produce a bias of under 0.01%. For
* values above this threshold, a slower but 100% accurate algorithm
* will be used.
*/
#ifndef RD_MAX_BIAS
#define RD_MAX_BIAS 0.0001
#endif /* RD_MAX_BIAS */
#ifndef RD_UNIFORM_THRESHOLD
#define RD_UNIFORM_THRESHOLD ((int)((double)(1u << 31) * 2.0 * RD_MAX_BIAS))
#endif /* RD_UNIFORM_THRESHOLD */
/*
* Generate a uniform integer distribution on the open interval
* [lower, upper). See comments above about RD_UNIFORM_THRESHOLD. If
* we are above the threshold, this function is relatively expensive
* because we may have to repeatedly draw random numbers to get a
* one that works.
*/
int32_t rds_iuniform(
mt_state * state, /* State of the MT PRNG to use */
int32_t lower, /* Lower limit of distribution */
int32_t upper) /* Upper limit of distribution */
{
uint32_t range = upper - lower;
/* Range of requested distribution */
if (range <= RD_UNIFORM_THRESHOLD)
return lower + (int32_t)(mts_ldrand(state) * range);
else
{
/*
* Using the simple formula would produce too much bias.
* Instead, draw numbers until we get one within the range.
* To save time, we first calculate a mask so that we only
* look at the number of bits we actually need. Since finding
* the mask is expensive, we optionally do a bit of caching
* here (note that the caching makes the code non-reentrant;
* set MT_CACHING to turn on this misfeature).
*
* Incidentally, the astute reader will note that we use the
* low-order bits of the PRNG output. If the PRNG were linear
* congruential, using the low-order bits wouuld be a major
* no-no. However, the Mersenne Twist PRNG doesn't have that
* drawback.
*/
#ifdef MT_CACHING
static uint32_t lastrange = 0; /* Range used last time */
static uint32_t rangemask = 0; /* Mask for range */
#else /* MT_CACHING */
uint32_t rangemask = 0; /* Mask for range */
#endif /* MT_CACHING */
register uint32_t
ranval; /* Random value from mts_lrand */
#ifdef MT_CACHING
if (range != lastrange)
#endif /* MT_CACHING */
{
/*
* Range is different from last time, recalculate mask.
*
* A few iterations could be trimmed off of the loop if we
* started rangemask at the next power of 2 above
* RD_UNIFORM_THRESHOLD. However, I don't currently know
* a formula for generating that value (though there is
* probably one in HAKMEM).
*/
#ifdef MT_CACHING
lastrange = range;
#endif /* MT_CACHING */
for (rangemask = 1;
rangemask < range && rangemask != 0;
rangemask <<= 1)
;
/*
* If rangemask became zero, the range is over 2^31. In
* that case, subtracting 1 from rangemask will produce a
* full-word mask, which is what we need.
*/
rangemask -= 1;
}
/*
* Draw random numbers until we get one in the requested range.
*/
do
{
ranval = mts_lrand(state) & rangemask;
}
while (ranval >= range);
return lower + ranval;
}
}
#ifdef INT64_MAX
/*
* Generate a uniform integer distribution on the half-open interval
* [lower, upper).
*/
int64_t rds_liuniform(
mt_state * state, /* State of the MT PRNG to use */
int64_t lower, /* Lower limit of distribution */
int64_t upper) /* Upper limit of distribution */
{
uint64_t range = upper - lower;
/* Range of requested distribution */
/*
* Draw numbers until we get one within the range. To save time,
* we first calculate a mask so that we only look at the number of
* bits we actually need. Since finding the mask is expensive, we
* optionally do a bit of caching here. See rds_iuniform for more
* information.
*/
#ifdef MT_CACHING
static uint32_t lastrange = 0; /* Range used last time */
static uint32_t rangemask = 0; /* Mask for range */
#else /* MT_CACHING */
uint32_t rangemask = 0; /* Mask for range */
#endif /* MT_CACHING */
register uint32_t ranval; /* Random value from mts_lrand */
#ifdef MT_CACHING
if (range != lastrange)
#endif /* MT_CACHING */
{
/*
* Range is different from last time, recalculate mask.
*/
#ifdef MT_CACHING
lastrange = range;
#endif /* MT_CACHING */
for (rangemask = 1;
rangemask < range && rangemask != 0;
rangemask <<= 1)
;
/*
* If rangemask became zero, the range is over 2^31. In
* that case, subtracting 1 from rangemask will produce a
* full-word mask, which is what we need.
*/
rangemask -= 1;
}
/*
* Draw random numbers until we get one in the requested range.
*/
do
{
ranval = mts_llrand(state) & rangemask;
}
while (ranval >= range);
return lower + ranval;
}
#endif /* INT64_MAX */
/*
* Generate a uniform distribution on the half-open interval [lower, upper).
*/
double rds_uniform(
mt_state * state, /* State of the MT PRNG to use */
double lower, /* Lower limit of distribution */
double upper) /* Upper limit of distribution */
{
return lower + mts_drand(state) * (upper - lower);
}
/*
* Generate a uniform distribution on the half-open interval [lower, upper).
*/
double rds_luniform(
mt_state * state, /* State of the MT PRNG to use */
double lower, /* Lower limit of distribution */
double upper) /* Upper limit of distribution */
{
return lower + mts_ldrand(state) * (upper - lower);
}
/*
* Generate an exponential distribution with the given mean.
*/
double rds_exponential(
mt_state * state, /* State of the MT PRNG to use */
double mean) /* Mean of generated distribution */
{
double random_value; /* Random sample on [0,1) */
do
random_value = mts_drand(state);
while (random_value == 0.0);
return -mean * log(random_value);
}
/*
* Generate an exponential distribution with the given mean.
*/
double rds_lexponential(
mt_state * state, /* State of the MT PRNG to use */
double mean) /* Mean of generated distribution */
{
double random_value; /* Random sample on [0,1) */
do
random_value = mts_ldrand(state);
while (random_value == 0.0);
return -mean * log(random_value);
}
/*
* Generate a p-Erlang distribution with the given mean.
*/
double rds_erlang(
mt_state * state, /* State of the MT PRNG to use */
int p, /* Order of distribution to generate */
double mean) /* Mean of generated distribution */
{
int order; /* Order generated so far */
double random_value; /* Value generated so far */
do
{
if (p <= 1)
p = 1;
random_value = mts_drand(state);
for (order = 1; order < p; order++)
random_value *= mts_drand(state);
}
while (random_value == 0.0);
return -mean * log(random_value) / p;
}
/*
* Generate a p-Erlang distribution with the given mean.
*/
double rds_lerlang(
mt_state * state, /* State of the MT PRNG to use */
int p, /* Order of distribution to generate */
double mean) /* Mean of generated distribution */
{
int order; /* Order generated so far */
double random_value; /* Value generated so far */
do
{
if (p <= 1)
p = 1;
random_value = mts_ldrand(state);
for (order = 1; order < p; order++)
random_value *= mts_ldrand(state);
}
while (random_value == 0.0);
return -mean * log(random_value) / p;
}
/*
* Generate a Weibull distribution with the given shape and scale parameters.
*/
double rds_weibull(
mt_state * state, /* State of the MT PRNG to use */
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
double random_value; /* Random sample on [0,1) */
do
random_value = mts_drand(state);
while (random_value == 0.0);
return scale * exp(log(-log(random_value)) / shape);
}
/* Weibull distribution */
/*
* Generate a Weibull distribution with the given shape and scale parameters.
*/
double rds_lweibull(
mt_state * state, /* State of the MT PRNG to use */
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
double random_value; /* Random sample on [0,1) */
do
random_value = mts_ldrand(state);
while (random_value == 0.0);
return scale * exp(log(-log(random_value)) / shape);
}
/* Weibull distribution */
/*
* Generate a normal distribution with the given mean and standard
* deviation. See Law and Kelton, p. 491.
*/
double rds_normal(
mt_state * state, /* State of the MT PRNG to use */
double mean, /* Mean of generated distribution */
double sigma) /* Standard deviation to generate */
{
double mag; /* Magnitude of (x,y) point */
double offset; /* Unscaled offset from mean */
double xranval; /* First random value on [-1,1) */
double yranval; /* Second random value on [-1,1) */
/*
* Generating a normal distribution is a bit tricky. We may need
* to make several attempts before we get a valid result. When we
* are done, we will have two normally distributed values, one of
* which we discard.
*/
do
{
xranval = 2.0 * mts_drand(state) - 1.0;
yranval = 2.0 * mts_drand(state) - 1.0;
mag = xranval * xranval + yranval * yranval;
}
while (mag > 1.0 || mag == 0.0);
offset = sqrt((-2.0 * log(mag)) / mag);
return mean + sigma * xranval * offset;
/*
* The second random variate is given by:
*
* mean + sigma * yranval * offset;
*
* If this were a C++ function, it could probably save that value
* somewhere and return it in the next subsequent call. But
* that's too hard to make bulletproof (and reentrant) in C.
*/
}
/*
* Generate a normal distribution with the given mean and standard
* deviation. See Law and Kelton, p. 491.
*/
double rds_lnormal(
mt_state * state, /* State of the MT PRNG to use */
double mean, /* Mean of generated distribution */
double sigma) /* Standard deviation to generate */
{
double mag; /* Magnitude of (x,y) point */
double offset; /* Unscaled offset from mean */
double xranval; /* First random value on [-1,1) */
double yranval; /* Second random value on [-1,1) */
/*
* Generating a normal distribution is a bit tricky. We may need
* to make several attempts before we get a valid result. When we
* are done, we will have two normally distributed values, one of
* which we discard.
*/
do
{
xranval = 2.0 * mts_ldrand(state) - 1.0;
yranval = 2.0 * mts_ldrand(state) - 1.0;
mag = xranval * xranval + yranval * yranval;
}
while (mag > 1.0 || mag == 0.0);
offset = sqrt((-2.0 * log(mag)) / mag);
return mean + sigma * xranval * offset;
/*
* The second random variate is given by:
*
* mean + sigma * yranval * offset;
*
* If this were a C++ function, it could probably save that value
* somewhere and return it in the next subsequent call. But
* that's too hard to make bulletproof (and reentrant) in C.
*/
}
/*
* Generate a lognormal distribution with the given shape and scale
* parameters.
*/
double rds_lognormal(
mt_state * state, /* State of the MT PRNG to use */
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
return exp(rds_normal(state, scale, shape));
}
/*
* Generate a lognormal distribution with the given shape and scale
* parameters.
*/
double rds_llognormal(
mt_state * state, /* State of the MT PRNG to use */
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
return exp(rds_lnormal(state, scale, shape));
}
/*
* Generate a triangular distibution between given limits, with a
* given mode.
*/
double rds_triangular(
mt_state * state, /* State of the MT PRNG to use */
double lower, /* Lower limit of distribution */
double upper, /* Upper limit of distribution */
double mode) /* Highest point of distribution */
{
double ran_value; /* Value generated by PRNG */
double scaled_mode; /* Scaled version of mode */
scaled_mode = (mode - lower) / (upper - lower);
ran_value = mts_drand(state);
if (ran_value <= scaled_mode)
ran_value = sqrt(scaled_mode * ran_value);
else
ran_value = 1.0 - sqrt((1.0 - scaled_mode) * (1.0 - ran_value));
return lower + (upper - lower) * ran_value;
}
/*
* Generate a triangular distibution between given limits, with a
* given mode.
*/
double rds_ltriangular(
mt_state * state, /* State of the MT PRNG to use */
double lower, /* Lower limit of distribution */
double upper, /* Upper limit of distribution */
double mode) /* Highest point of distribution */
{
double ran_value; /* Value generated by PRNG */
double scaled_mode; /* Scaled version of mode */
scaled_mode = (mode - lower) / (upper - lower);
ran_value = mts_ldrand(state);
if (ran_value <= scaled_mode)
ran_value = sqrt(scaled_mode * ran_value);
else
ran_value = 1.0 - sqrt((1.0 - scaled_mode) * (1.0 - ran_value));
return lower + (upper - lower) * ran_value;
}
/*
* Generate a discrete integer empirical distribution given a set of
* probability cutoffs. See rd_empirical_setup for full information.
*/
size_t rds_int_empirical(
mt_state * state, /* State of the MT PRNG to use */
rd_empirical_control* control) /* Control from rd_empirical_setup */
{
double ran_value; /* Value generated by PRNG */
size_t result; /* Result we'll return */
ran_value = mts_ldrand(state);
ran_value *= control->n; /* Scale value to required range */
result = (size_t)ran_value; /* Integer part MIGHT be result */
if (ran_value < control->cutoff[result]) /* Correct probability? */
return result; /* Done! */
else
return control->remap[result]; /* Nope, remap to correct result */
}
/*
* Generate a discrete floating-point empirical distribution given a
* set of probability cutoffs. Use the result of rds_int_empirical to
* choose a final value.
*/
double rds_double_empirical(
mt_state * state, /* State of the MT PRNG to use */
rd_empirical_control* control) /* Control from rd_empirical_setup */
{
return control->values[rds_int_empirical(state, control)];
}
/*
* Generate a continuous floating-point empirical distribution given a
* set of probability cutoffs. Use the result of rds_int_empirical to
* choose a pair of values, and then return a uniform distribution
* between those two values.
*/
double rds_continuous_empirical(
mt_state * state, /* State of the MT PRNG to use */
rd_empirical_control* control) /* Control from rd_empirical_setup */
{
size_t index; /* Index into values table */
index = rds_int_empirical(state, control);
return control->values[index]
+ mts_ldrand(state)
* (control->values[index + 1] - control->values[index]);
}
/*
* Generate a uniform integer distribution on the half-open interval
* [lower, upper). See comments on rds_iuniform.
*/
int32_t rd_iuniform(
int32_t lower, /* Lower limit of distribution */
int32_t upper) /* Upper limit of distribution */
{
return rds_iuniform(&mt_default_state, lower, upper);
}
#ifdef INT64_MAX
/*
* Generate a uniform integer distribution on the open interval
* [lower, upper). See comments on rds_iuniform.
*/
int64_t rd_liuniform(
int64_t lower, /* Lower limit of distribution */
int64_t upper) /* Upper limit of distribution */
{
return rds_liuniform(&mt_default_state, lower, upper);
}
#endif /* INT64_MAX */
/*
* Generate a uniform distribution on the open interval [lower, upper).
*/
double rd_uniform(
double lower, /* Lower limit of distribution */
double upper) /* Upper limit of distribution */
{
return rds_uniform (&mt_default_state, lower, upper);
}
/*
* Generate a uniform distribution on the open interval [lower, upper).
*/
double rd_luniform(
double lower, /* Lower limit of distribution */
double upper) /* Upper limit of distribution */
{
return rds_luniform (&mt_default_state, lower, upper);
}
/*
* Generate an exponential distribution with the given mean.
*/
double rd_exponential(
double mean) /* Mean of generated distribution */
{
return rds_exponential (&mt_default_state, mean);
}
/*
* Generate an exponential distribution with the given mean.
*/
double rd_lexponential(
double mean) /* Mean of generated distribution */
{
return rds_lexponential (&mt_default_state, mean);
}
/*
* Generate a p-Erlang distribution with the given mean.
*/
double rd_erlang(
int p, /* Order of distribution to generate */
double mean) /* Mean of generated distribution */
{
return rds_erlang (&mt_default_state, p, mean);
}
/*
* Generate a p-Erlang distribution with the given mean.
*/
double rd_lerlang(
int p, /* Order of distribution to generate */
double mean) /* Mean of generated distribution */
{
return rds_lerlang (&mt_default_state, p, mean);
}
/*
* Generate a Weibull distribution with the given shape and scale parameters.
*/
double rd_weibull(
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
return rds_weibull (&mt_default_state, shape, scale);
}
/*
* Generate a Weibull distribution with the given shape and scale parameters.
*/
double rd_lweibull(
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
return rds_lweibull (&mt_default_state, shape, scale);
}
/*
* Generate a normal distribution with the given mean and standard
* deviation. See Law and Kelton, p. 491.
*/
double rd_normal(
double mean, /* Mean of generated distribution */
double sigma) /* Standard deviation to generate */
{
return rds_normal (&mt_default_state, mean, sigma);
}
/*
* Generate a normal distribution with the given mean and standard
* deviation. See Law and Kelton, p. 491.
*/
double rd_lnormal(
double mean, /* Mean of generated distribution */
double sigma) /* Standard deviation to generate */
{
return rds_lnormal (&mt_default_state, mean, sigma);
}
/*
* Generate a lognormal distribution with the given shape and scale
* parameters.
*/
double rd_lognormal(
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
return rds_lognormal (&mt_default_state, shape, scale);
}
/*
* Generate a lognormal distribution with the given shape and scale
* parameters.
*/
double rd_llognormal(
double shape, /* Shape of the distribution */
double scale) /* Scale of the distribution */
{
return rds_llognormal (&mt_default_state, shape, scale);
}
/*
* Generate a triangular distibution between given limits, with a
* given mode.
*/
double rd_triangular(
double lower, /* Lower limit of distribution */
double upper, /* Upper limit of distribution */
double mode)
{
return rds_triangular (&mt_default_state, lower, upper, mode);
}
/*
* Generate a triangular distibution between given limits, with a
* given mode.
*/
double rd_ltriangular(
double lower, /* Lower limit of distribution */
double upper, /* Upper limit of distribution */
double mode)
{
return rds_ltriangular (&mt_default_state, lower, upper, mode);
}
/*
* Set up to calculate an empirical distribution in O(1) time. The
* method used is adapted from Alastair J. Walker, "An efficient
* method for generating discrete random variables with general
* distributions", ACM Transactions on Mathematical Software 3,
* 253-256 (1977). Walker's algorithm required O(N^2) setup time;
* this code uses the O(N) setup approach devised by James Theiler of
* LANL, as documented in commentary ini the Gnu Scientific Library.
* We also use a modification suggested by Donald E. Knuth, The Art of
* Computer Programming, Volume 2 (Seminumerical algorithms), 3rd
* edition, Addison-Wesley (1997), p120.
*
* The essence of Walker's approach is to observe that each empirical
* probabilitiy is either above or below the uniform probability by
* some amount. Suppose the probability pi of the i-th element is
* smaller than the uniform probability (1/n). Then if we choose a
* uniformly distributed random integer, i will appear too often; to
* be precise, it will appear 1/n - pi too frequently. Walker's idea
* is that there must be some other element, j, that has a probability
* pj that is above uniform. So if we "push" the 1/n - pi "extra"
* probability of element i onto element j, we will decrease the
* probability of i appearing and increase the probability of j. We
* can do this by selecting a "cutoff" value which is to be compared
* to a random number x on [0,1); if x exceeds the cutoff, we remap to
* element j. The cutoff is selected such that this happens exactly
* (1/n - pi) / (1/n) = 1 - n*pi of the time, since that's the amount
* of extra probability that needs to be pushed onto j.
*
* For example, suppose there are only two probabilities, 0.25 and
* 0.75. Element 0 will be selected 0.5 of the time, so we must remap
* half of those selections to j. The cutoff is chosen as 1 - 2*0.25
* = 0.5. Presto!
*
* In general, element j won't need precisely the amount of extra
* stuff remapped from element i. If it needs more, that's OK; there
* will be some other element k that has a probability below uniform,
* and we can also map its extra onto j. If j needs *less* extra,
* then we'll do a remap on it as well, pushing that extra onto yet
* another element--but only if j was selected directly in the initial
* uniform distribution. (All of these adjustments are done by
* modifying the calculated difference between j's probability and the
* uniform distribution.) This produces the rather odd result that j
* both accepts and donates probability, but it all works out in the
* end.
*
* The trick is then to calculate the cutoff and remap arrays. The
* formula for the cutoff values was given above. At each step,
* Walker scans the current probability array to find the elements
* that are most "out of balance" on both the high and low ends; the
* low one is then remapped to the high. The loop is repeated until
* all probabilities differ from uniform by less than predetermined
* threshold. This is an O(N^2) algorithm; it can also be troublesome
* if the threshold is in appropriate for the data at hand.
*
* Theiler's improvement involves noting that if a probability is
* below uniform ("small"), it will never become "large". That means
* we can keep two tables, one each of small and large values. For
* convenience, the tables are organized as stacks. At each step, a
* value is popped from each stack, and the small one is remapped to
* the large one by calculating a cutoff. The large value is then
* placed back on the appropriate stack. (For efficiency, the
* implementation doesn't pop from the large stack unless necessary.)
*
* Finally, Knuth's improvements: Walker's original paper suggested
* drawing two uniform random numbers when generating from the
* empirical distribution: one to select an element, and a second to
* compare to the cutoff. Knuth points out that if the random numbers
* have sufficient entropy (which is certainly true for the Mersenne
* Twister), we can use the upper bits to choose a slot and the lower
* ones to compare against the cutoff. This is done by taking s = n*r
* (where r is the double-precision random value), and then using
* int(s) as the slot and frac(s) as the cutoff. The final
* improvement is that we can avoid calculating frac(s) if, when
* setting the cutoff c, we store i + c instead of c, where i is the
* slot number.
*/
rd_empirical_control* rd_empirical_setup(
size_t n_probs, /* Number of probabilities provide */
double* probs, /* Probability (weight) table */
double* values) /* Value for floating distributions */
{
rd_empirical_control* control; /* Control structure we'll build */
size_t i; /* General loop index */
size_t j; /* Element from stack_high */
size_t n_high; /* Current size of stack_high */
size_t n_low; /* Current size of stack_low */
size_t* stack_high; /* Stack of values above uniform */
size_t* stack_low; /* Stack of values below uniform */
double prob_total; /* Total of all weights */
control = (rd_empirical_control*)malloc(sizeof *control);
if (control == NULL)
return NULL;
control->n = n_probs;