1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rng.sampling.distribution;
19
20 import org.apache.commons.rng.UniformRandomProvider;
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 public abstract class ZigguratSampler implements SharedStateContinuousSampler {
61
62 private static final int MASK_INT8 = 0xff;
63
64 private static final long MAX_INT64 = Long.MAX_VALUE;
65
66 private static final double TWO_POW_63 = 0x1.0p63;
67
68
69 private final UniformRandomProvider rng;
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 public static class Exponential extends ZigguratSampler {
259
260
261
262
263
264
265
266 private static final int I_MAX = 252;
267
268
269 private static final long E_MAX = 853965788476313647L;
270
271 private static final double X_0 = 7.569274694148063;
272
273
274
275
276 private static final byte[] MAP = {
277 (byte) 0, (byte) 0, (byte) 1, (byte) 235, (byte) 3, (byte) 4, (byte) 5, (byte) 0,
278 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
279 (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 2, (byte) 2,
280 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
281 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
282 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
283 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
284 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
285 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
286 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
287 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
288 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
289 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
290 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
291 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
292 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
293 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
294 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
295 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
296 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
297 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
298 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
299 (byte) 252, (byte) 251, (byte) 251, (byte) 251, (byte) 251, (byte) 251, (byte) 251, (byte) 251,
300 (byte) 251, (byte) 251, (byte) 251, (byte) 251, (byte) 251, (byte) 251, (byte) 250, (byte) 250,
301 (byte) 250, (byte) 250, (byte) 250, (byte) 250, (byte) 250, (byte) 249, (byte) 249, (byte) 249,
302 (byte) 249, (byte) 249, (byte) 249, (byte) 248, (byte) 248, (byte) 248, (byte) 248, (byte) 247,
303 (byte) 247, (byte) 247, (byte) 247, (byte) 246, (byte) 246, (byte) 246, (byte) 245, (byte) 245,
304 (byte) 244, (byte) 244, (byte) 243, (byte) 243, (byte) 242, (byte) 241, (byte) 241, (byte) 240,
305 (byte) 239, (byte) 237, (byte) 3, (byte) 3, (byte) 4, (byte) 4, (byte) 6, (byte) 0,
306 (byte) 0, (byte) 0, (byte) 0, (byte) 236, (byte) 237, (byte) 238, (byte) 239, (byte) 240,
307 (byte) 241, (byte) 242, (byte) 243, (byte) 244, (byte) 245, (byte) 246, (byte) 247, (byte) 248,
308 (byte) 249, (byte) 250, (byte) 251, (byte) 252, (byte) 2, (byte) 0, (byte) 0, (byte) 0,
309 };
310
311
312
313
314 private static final long[] IPMF = {
315 9223372036854774016L, 1623796909450834944L, 2664290944894291200L, 7387971354164060928L,
316 6515064486552723200L, 8840508362680718848L, 6099647593382936320L, 7673130333659513856L,
317 6220332867583438080L, 5045979640552813824L, 4075305837223955456L, 3258413672162525440L,
318 2560664887087762432L, 1957224924672899584L, 1429800935350577408L, 964606309710808320L,
319 551043923599587072L, 180827629096890368L, -152619738120023552L, -454588624410291456L,
320 -729385126147774976L, -980551509819447040L, -1211029700667463936L, -1423284293868548352L,
321 -1619396356369050368L, -1801135830956211712L, -1970018048575618048L, -2127348289059705344L,
322 -2274257249303686400L, -2411729520096655360L, -2540626634159181056L, -2661705860113406464L,
323 -2775635634532450560L, -2883008316030465280L, -2984350790383654912L, -3080133339198116352L,
324 -3170777096303091200L, -3256660348483819008L, -3338123885075136256L, -3415475560473299200L,
325 -3488994201966428160L, -3558932970354473216L, -3625522261068041216L, -3688972217741989376L,
326 -3749474917563782656L, -3807206277531056128L, -3862327722496843520L, -3914987649156779776L,
327 -3965322714631865344L, -4013458973776895488L, -4059512885612783360L, -4103592206186241024L,
328 -4145796782586128128L, -4186219260694347008L, -4224945717447275264L, -4262056226866285568L,
329 -4297625367836519680L, -4331722680528537344L, -4364413077437472512L, -4395757214229401600L,
330 -4425811824915135744L, -4454630025296932608L, -4482261588141290496L, -4508753193105288192L,
331 -4534148654077808896L, -4558489126279958272L, -4581813295192216576L, -4604157549138257664L,
332 -4625556137145255168L, -4646041313519104512L, -4665643470413305856L, -4684391259530326528L,
333 -4702311703971761664L, -4719430301145103360L, -4735771117539946240L, -4751356876102087168L,
334 -4766209036859133952L, -4780347871386013440L, -4793792531638892032L, -4806561113635132672L,
335 -4818670716409306624L, -4830137496634465536L, -4840976719260837888L, -4851202804490348800L,
336 -4860829371376460032L, -4869869278311657472L, -4878334660640771072L, -4886236965617427200L,
337 -4893586984900802560L, -4900394884772702720L, -4906670234238885376L, -4912422031164496896L,
338 -4917658726580119808L, -4922388247283532288L, -4926618016851066624L, -4930354975163335168L,
339 -4933605596540651264L, -4936375906575303936L, -4938671497741366016L, -4940497543854575616L,
340 -4941858813449629440L, -4942759682136114944L, -4943204143989086720L, -4943195822025528064L,
341 -4942737977813206528L, -4941833520255033344L, -4940485013586738944L, -4938694684624359424L,
342 -4936464429291795968L, -4933795818458825728L, -4930690103114057984L, -4927148218896864000L,
343 -4923170790008275968L, -4918758132519213568L, -4913910257091645696L, -4908626871126539264L,
344 -4902907380349533952L, -4896750889844272896L, -4890156204540531200L, -4883121829162554368L,
345 -4875645967641781248L, -4867726521994927104L, -4859361090668103424L, -4850546966345113600L,
346 -4841281133215539200L, -4831560263698491904L, -4821380714613447424L, -4810738522790066176L,
347 -4799629400105481984L, -4788048727936307200L, -4775991551010514944L, -4763452570642114304L,
348 -4750426137329494528L, -4736906242696389120L, -4722886510751377664L, -4708360188440089088L,
349 -4693320135461421056L, -4677758813316108032L, -4661668273553489152L, -4645040145179241472L,
350 -4627865621182772224L, -4610135444140930048L, -4591839890849345536L, -4572968755929961472L,
351 -4553511334358205696L, -4533456402849101568L, -4512792200036279040L, -4491506405372580864L,
352 -4469586116675402496L, -4447017826233107968L, -4423787395382284800L, -4399880027458416384L,
353 -4375280239014115072L, -4349971829190472192L, -4323937847117721856L, -4297160557210933504L,
354 -4269621402214949888L, -4241300963840749312L, -4212178920821861632L, -4182234004204451584L,
355 -4151443949668877312L, -4119785446662287616L, -4087234084103201536L, -4053764292396156928L,
356 -4019349281473081856L, -3983960974549692672L, -3947569937258423296L, -3910145301787345664L,
357 -3871654685619032064L, -3832064104425388800L, -3791337878631544832L, -3749438533114327552L,
358 -3706326689447984384L, -3661960950051848192L, -3616297773528534784L, -3569291340409189376L,
359 -3520893408440946176L, -3471053156460654336L, -3419717015797782528L, -3366828488034805504L,
360 -3312327947826460416L, -3256152429334010368L, -3198235394669719040L, -3138506482563172864L,
361 -3076891235255162880L, -3013310801389730816L, -2947681612411374848L, -2879915029671670784L,
362 -2809916959107513856L, -2737587429961866240L, -2662820133571325696L, -2585501917733380096L,
363 -2505512231579385344L, -2422722515205211648L, -2336995527534088448L, -2248184604988727552L,
364 -2156132842510765056L, -2060672187261025536L, -1961622433929371904L, -1858790108950105600L,
365 -1751967229002895616L, -1640929916937142784L, -1525436855617582592L, -1405227557075253248L,
366 -1280020420662650112L, -1149510549536596224L, -1013367289578704896L, -871231448632104192L,
367 -722712146453667840L, -567383236774436096L, -404779231966938368L, -234390647591545856L,
368 -55658667960119296L, 132030985907841280L, 329355128892811776L, 537061298001085184L,
369 755977262693564160L, 987022116608033280L, 1231219266829431296L, 1489711711346518528L,
370 1763780090187553792L, 2054864117341795072L, 2364588157623768832L, 2694791916990503168L,
371 3047567482883476224L, 3425304305830816256L, 3830744187097297920L, 4267048975685830400L,
372 4737884547990017280L, 5247525842198998272L, 5800989391535355392L, 6404202162993295360L,
373 7064218894258540544L, 7789505049452331520L, 8590309807749444864L, 7643763810684489984L,
374 8891950541491446016L, 5457384281016206080L, 9083704440929284096L, 7976211653914433280L,
375 8178631350487117568L, 2821287825726744832L, 6322989683301709568L, 4309503753387611392L,
376 4685170734960170496L, 8404845967535199744L, 7330522972447554048L, 1960945799076992000L,
377 4742910674644899072L, -751799822533509888L, 7023456603741959936L, 3843116882594676224L,
378 3927231442413903104L, -9223372036854775808L, -9223372036854775808L, -9223372036854775808L,
379 };
380
381
382
383
384
385
386
387
388
389
390 private static final double[] X = {
391 8.2066240675348816e-19, 7.3973732351607284e-19, 6.9133313377915293e-19, 6.5647358820964533e-19,
392 6.2912539959818508e-19, 6.0657224129604964e-19, 5.8735276103737269e-19, 5.7058850528536941e-19,
393 5.557094569162239e-19, 5.4232438903743953e-19, 5.3015297696508776e-19, 5.1898739257708062e-19,
394 5.086692261799833e-19, 4.9907492938796469e-19, 4.9010625894449536e-19, 4.8168379010649187e-19,
395 4.7374238653644714e-19, 4.6622795807196824e-19, 4.5909509017784048e-19, 4.5230527790658154e-19,
396 4.458255881635396e-19, 4.3962763126368381e-19, 4.336867596710647e-19, 4.2798143618469714e-19,
397 4.2249273027064889e-19, 4.172039125346411e-19, 4.1210012522465616e-19, 4.0716811225869233e-19,
398 4.0239599631006903e-19, 3.9777309342877357e-19, 3.9328975785334499e-19, 3.8893725129310323e-19,
399 3.8470763218720385e-19, 3.8059366138180143e-19, 3.765887213854473e-19, 3.7268674692030177e-19,
400 3.6888216492248162e-19, 3.6516984248800068e-19, 3.6154504153287473e-19, 3.5800337915318032e-19,
401 3.5454079284533432e-19, 3.5115350988784242e-19, 3.4783802030030962e-19, 3.4459105288907336e-19,
402 3.4140955396563316e-19, 3.3829066838741162e-19, 3.3523172262289001e-19, 3.3223020958685874e-19,
403 3.2928377502804472e-19, 3.2639020528202049e-19, 3.2354741622810815e-19, 3.2075344331080789e-19,
404 3.1800643250478609e-19, 3.1530463211820845e-19, 3.1264638534265134e-19, 3.1003012346934211e-19,
405 3.0745435970137301e-19, 3.0491768350005559e-19, 3.0241875541094565e-19, 2.999563023214455e-19,
406 2.9752911310742592e-19, 2.9513603463113224e-19, 2.9277596805684267e-19, 2.9044786545442563e-19,
407 2.8815072666416712e-19, 2.8588359639906928e-19, 2.8364556156331615e-19, 2.8143574876779799e-19,
408 2.7925332202553125e-19, 2.7709748061152879e-19, 2.7496745707320232e-19, 2.7286251537873397e-19,
409 2.7078194919206054e-19, 2.687250802641905e-19, 2.6669125693153442e-19, 2.6467985271278891e-19,
410 2.6269026499668434e-19, 2.6072191381359757e-19, 2.5877424068465143e-19, 2.5684670754248168e-19,
411 2.5493879571835479e-19, 2.5305000499077481e-19, 2.511798526911271e-19, 2.4932787286227806e-19,
412 2.474936154663866e-19, 2.4567664563848669e-19, 2.4387654298267842e-19, 2.4209290090801527e-19,
413 2.4032532600140538e-19, 2.3857343743505147e-19, 2.3683686640614648e-19, 2.3511525560671253e-19,
414 2.3340825872163284e-19, 2.3171553995306794e-19, 2.3003677356958333e-19, 2.2837164347843482e-19,
415 2.2671984281957174e-19, 2.2508107358001938e-19, 2.2345504622739592e-19, 2.2184147936140775e-19,
416 2.2024009938224424e-19, 2.1865064017486842e-19, 2.1707284280826716e-19, 2.1550645524878675e-19,
417 2.1395123208673778e-19, 2.124069342755064e-19, 2.1087332888245875e-19, 2.0935018885097035e-19,
418 2.0783729277295508e-19, 2.0633442467130712e-19, 2.0484137379170616e-19, 2.0335793440326865e-19,
419 2.018839056075609e-19, 2.0041909115551697e-19, 1.9896329927183254e-19, 1.975163424864309e-19,
420 1.9607803747261946e-19, 1.9464820489157862e-19, 1.9322666924284314e-19, 1.9181325872045647e-19,
421 1.9040780507449479e-19, 1.8901014347767504e-19, 1.8762011239677479e-19, 1.8623755346860768e-19,
422 1.8486231138030984e-19, 1.8349423375370566e-19, 1.8213317103353295e-19, 1.8077897637931708e-19,
423 1.7943150556069476e-19, 1.7809061685599652e-19, 1.7675617095390567e-19, 1.7542803085801941e-19,
424 1.7410606179414531e-19, 1.727901311201724e-19, 1.7148010823836362e-19, 1.7017586450992059e-19,
425 1.6887727317167824e-19, 1.6758420925479093e-19, 1.6629654950527621e-19, 1.6501417230628659e-19,
426 1.6373695760198277e-19, 1.624647868228856e-19, 1.6119754281258616e-19, 1.5993510975569615e-19,
427 1.5867737310692309e-19, 1.5742421952115544e-19, 1.5617553678444595e-19, 1.5493121374578016e-19,
428 1.5369114024951992e-19, 1.5245520706841019e-19, 1.5122330583703858e-19, 1.4999532898563561e-19,
429 1.4877116967410352e-19, 1.4755072172615974e-19, 1.4633387956347966e-19, 1.4512053813972103e-19,
430 1.4391059287430991e-19, 1.4270393958586506e-19, 1.4150047442513381e-19, 1.4030009380730888e-19,
431 1.3910269434359025e-19, 1.3790817277185197e-19, 1.3671642588626657e-19, 1.3552735046573446e-19,
432 1.3434084320095729e-19, 1.3315680061998685e-19, 1.3197511901207148e-19, 1.3079569434961214e-19,
433 1.2961842220802957e-19, 1.2844319768333099e-19, 1.2726991530715219e-19, 1.2609846895903523e-19,
434 1.2492875177568625e-19, 1.237606560569394e-19, 1.2259407316813331e-19, 1.2142889343858445e-19,
435 1.2026500605581765e-19, 1.1910229895518744e-19, 1.1794065870449425e-19, 1.1677997038316715e-19,
436 1.1562011745554883e-19, 1.1446098163777869e-19, 1.1330244275772562e-19, 1.1214437860737343e-19,
437 1.109866647870073e-19, 1.0982917454048923e-19, 1.0867177858084351e-19, 1.0751434490529747e-19,
438 1.0635673859884002e-19, 1.0519882162526621e-19, 1.0404045260457141e-19, 1.0288148657544097e-19,
439 1.0172177474144965e-19, 1.0056116419943559e-19, 9.9399497648346677e-20, 9.8236613076667446e-20,
440 9.7072343426320094e-20, 9.5906516230690634e-20, 9.4738953224154196e-20, 9.3569469920159036e-20,
441 9.2397875154569468e-20, 9.1223970590556472e-20, 9.0047550180852874e-20, 8.8868399582647627e-20,
442 8.768629551976745e-20, 8.6501005086071005e-20, 8.5312284983141187e-20, 8.4119880684385214e-20,
443 8.292352551651342e-20, 8.1722939648034506e-20, 8.0517828972839211e-20, 7.9307883875099226e-20,
444 7.8092777859524425e-20, 7.6872166028429042e-20, 7.5645683383965122e-20, 7.4412942930179128e-20,
445 7.3173533545093332e-20, 7.1927017587631075e-20, 7.0672928197666785e-20, 6.9410766239500362e-20,
446 6.8139996829256425e-20, 6.6860045374610234e-20, 6.5570293040210081e-20, 6.4270071533368528e-20,
447 6.2958657080923559e-20, 6.1635263438143136e-20, 6.02990337321517e-20, 5.8949030892850181e-20,
448 5.758422635988593e-20, 5.6203486669597397e-20, 5.4805557413499315e-20, 5.3389043909003295e-20,
449 5.1952387717989917e-20, 5.0493837866338355e-20, 4.9011415222629489e-20, 4.7502867933366117e-20,
450 4.5965615001265455e-20, 4.4396673897997565e-20, 4.2792566302148588e-20, 4.1149193273430015e-20,
451 3.9461666762606287e-20, 3.7724077131401685e-20, 3.592916408620436e-20, 3.4067836691100565e-20,
452 3.2128447641564046e-20, 3.0095646916399994e-20, 2.7948469455598328e-20, 2.5656913048718645e-20,
453 2.3175209756803909e-20, 2.0426695228251291e-20, 1.7261770330213488e-20, 1.3281889259442579e-20,
454 0,
455 };
456
457
458
459
460
461
462
463
464
465
466 private static final double[] Y = {
467 5.595205495112736e-23, 1.1802509982703313e-22, 1.8444423386735829e-22, 2.5439030466698309e-22,
468 3.2737694311509334e-22, 4.0307732132706715e-22, 4.8125478319495115e-22, 5.6172914896583308e-22,
469 6.4435820540443526e-22, 7.2902662343463681e-22, 8.1563888456321941e-22, 9.0411453683482223e-22,
470 9.9438488486399206e-22, 1.0863906045969114e-21, 1.1800799775461269e-21, 1.2754075534831208e-21,
471 1.372333117637729e-21, 1.4708208794375214e-21, 1.5708388257440445e-21, 1.6723581984374566e-21,
472 1.7753530675030514e-21, 1.8797999785104595e-21, 1.9856776587832504e-21, 2.0929667704053244e-21,
473 2.201649700995824e-21, 2.3117103852306179e-21, 2.4231341516125464e-21, 2.5359075901420891e-21,
474 2.6500184374170538e-21, 2.7654554763660391e-21, 2.8822084483468604e-21, 3.0002679757547711e-21,
475 3.1196254936130377e-21, 3.2402731888801749e-21, 3.3622039464187092e-21, 3.4854113007409036e-21,
476 3.6098893927859475e-21, 3.7356329310971768e-21, 3.8626371568620053e-21, 3.9908978123552837e-21,
477 4.1204111123918948e-21, 4.2511737184488913e-21, 4.3831827151633737e-21, 4.5164355889510656e-21,
478 4.6509302085234806e-21, 4.7866648071096003e-21, 4.9236379662119969e-21, 5.0618486007478993e-21,
479 5.2012959454434732e-21, 5.3419795423648946e-21, 5.4838992294830959e-21, 5.6270551301806347e-21,
480 5.7714476436191935e-21, 5.9170774358950678e-21, 6.0639454319177027e-21, 6.2120528079531677e-21,
481 6.3614009847804375e-21, 6.5119916214136427e-21, 6.6638266093481696e-21, 6.8169080672926277e-21,
482 6.9712383363524377e-21, 7.1268199756340822e-21, 7.2836557582420336e-21, 7.4417486676430174e-21,
483 7.6011018943746355e-21, 7.7617188330775411e-21, 7.9236030798322572e-21, 8.0867584297834842e-21,
484 8.2511888750363333e-21, 8.4168986028103258e-21, 8.5838919938383098e-21, 8.7521736209986459e-21,
485 8.9217482481700712e-21, 9.0926208292996504e-21, 9.2647965076751277e-21, 9.4382806153938292e-21,
486 9.6130786730210328e-21, 9.7891963894314161e-21, 9.966639661827884e-21, 1.0145414575932636e-20,
487 1.0325527406345955e-20, 1.0506984617068672e-20, 1.0689792862184811e-20, 1.0873958986701341e-20,
488 1.10594900275424e-20, 1.1246393214695825e-20, 1.1434675972510121e-20, 1.1624345921140471e-20,
489 1.1815410878142659e-20, 1.2007878860214202e-20, 1.2201758085082226e-20, 1.239705697353804e-20,
490 1.2593784151618565e-20, 1.2791948452935152e-20, 1.29915589211506e-20, 1.3192624812605428e-20,
491 1.3395155599094805e-20, 1.3599160970797774e-20, 1.3804650839360727e-20, 1.4011635341137284e-20,
492 1.4220124840587164e-20, 1.4430129933836705e-20, 1.4641661452404201e-20, 1.485473046709328e-20,
493 1.5069348292058084e-20, 1.5285526489044053e-20, 1.5503276871808626e-20, 1.5722611510726402e-20,
494 1.5943542737583543e-20, 1.6166083150566702e-20, 1.6390245619451956e-20, 1.6616043290999594e-20,
495 1.6843489594561079e-20, 1.7072598247904713e-20, 1.7303383263267072e-20, 1.7535858953637607e-20,
496 1.7770039939284241e-20, 1.8005941154528286e-20, 1.8243577854777398e-20, 1.8482965623825808e-20,
497 1.8724120381431627e-20, 1.8967058391181452e-20, 1.9211796268653192e-20, 1.9458350989888484e-20,
498 1.9706739900186868e-20, 1.9956980723234356e-20, 2.0209091570579904e-20, 2.0463090951473895e-20,
499 2.0718997783083593e-20, 2.097683140110135e-20, 2.123661157076213e-20, 2.1498358498287976e-20,
500 2.1762092842777868e-20, 2.2027835728562592e-20, 2.2295608758045219e-20, 2.2565434025049041e-20,
501 2.2837334128696004e-20, 2.311133218784001e-20, 2.3387451856080863e-20, 2.3665717337386111e-20,
502 2.394615340234961e-20, 2.422878540511741e-20, 2.4513639301013211e-20, 2.4800741664897764e-20,
503 2.5090119710298442e-20, 2.5381801309347597e-20, 2.56758150135705e-20, 2.5972190075566336e-20,
504 2.6270956471628253e-20, 2.6572144925351523e-20, 2.6875786932281841e-20, 2.7181914785659148e-20,
505 2.7490561603315974e-20, 2.7801761355793055e-20, 2.8115548895739172e-20, 2.8431959988666534e-20,
506 2.8751031345137833e-20, 2.9072800654466307e-20, 2.9397306620015486e-20, 2.9724588996191657e-20,
507 3.0054688627228112e-20, 3.0387647487867642e-20, 3.0723508726057078e-20, 3.1062316707775905e-20,
508 3.1404117064129991e-20, 3.1748956740850969e-20, 3.2096884050352357e-20, 3.2447948726504914e-20,
509 3.2802201982306013e-20, 3.3159696570631373e-20, 3.352048684827223e-20, 3.3884628843476888e-20,
510 3.4252180327233346e-20, 3.4623200888548644e-20, 3.4997752014001677e-20, 3.537589717186906e-20,
511 3.5757701901149035e-20, 3.6143233905835799e-20, 3.65325631548274e-20, 3.6925761987883572e-20,
512 3.7322905228086981e-20, 3.7724070301302117e-20, 3.8129337363171041e-20, 3.8538789434235234e-20,
513 3.8952512543827862e-20, 3.9370595883442399e-20, 3.9793131970351439e-20, 4.0220216822325769e-20,
514 4.0651950144388133e-20, 4.1088435528630944e-20, 4.1529780668232712e-20, 4.1976097586926582e-20,
515 4.2427502885307452e-20, 4.2884118005513604e-20, 4.3346069515987453e-20, 4.3813489418210257e-20,
516 4.4286515477520838e-20, 4.4765291580372353e-20, 4.5249968120658306e-20, 4.5740702418054417e-20,
517 4.6237659171683015e-20, 4.6741010952818368e-20, 4.7250938740823415e-20, 4.7767632507051219e-20,
518 4.8291291852069895e-20, 4.8822126702292804e-20, 4.9360358072933852e-20, 4.9906218905182021e-20,
519 5.0459954986625539e-20, 5.1021825965285324e-20, 5.1592106469178258e-20, 5.2171087345169234e-20,
520 5.2759077033045284e-20, 5.3356403093325858e-20, 5.3963413910399511e-20, 5.4580480596259246e-20,
521 5.5207999124535584e-20, 5.584639272987383e-20, 5.649611461419377e-20, 5.7157651009290713e-20,
522 5.7831524654956632e-20, 5.8518298763794323e-20, 5.9218581558791713e-20, 5.99330314883387e-20,
523 6.0662363246796887e-20, 6.1407354758435e-20, 6.2168855320499763e-20, 6.2947795150103727e-20,
524 6.3745196643214394e-20, 6.4562187737537985e-20, 6.5400017881889097e-20, 6.6260077263309343e-20,
525 6.714392014514662e-20, 6.8053293447301698e-20, 6.8990172088133e-20, 6.9956803158564498e-20,
526 7.095576179487843e-20, 7.199002278894508e-20, 7.3063053739105458e-20, 7.4178938266266881e-20,
527 7.5342542134173124e-20, 7.6559742171142969e-20, 7.783774986341285e-20, 7.9185582674029512e-20,
528 8.06147755373533e-20, 8.2140502769818073e-20, 8.3783445978280519e-20, 8.5573129249678161e-20,
529 8.75544596695901e-20, 8.9802388057706877e-20, 9.2462471421151086e-20, 9.5919641344951721e-20,
530 1.0842021724855044e-19,
531 };
532
533
534
535
536 private static final class ExponentialMean extends Exponential {
537
538 private final double mean;
539
540
541
542
543
544 ExponentialMean(UniformRandomProvider rng, double mean) {
545 super(rng);
546 this.mean = mean;
547 }
548
549 @Override
550 public double sample() {
551 return super.sample() * mean;
552 }
553
554 @Override
555 public ExponentialMean withUniformRandomProvider(UniformRandomProvider rng) {
556 return new ExponentialMean(rng, this.mean);
557 }
558 }
559
560
561
562
563 Exponential(UniformRandomProvider rng) {
564 super(rng);
565 }
566
567
568 @Override
569 public String toString() {
570 return toString("exponential");
571 }
572
573
574 @Override
575 public double sample() {
576
577
578
579 final long x = nextLong();
580
581 final int i = ((int) x) & MASK_INT8;
582
583 if (i < I_MAX) {
584
585
586
587 return X[i] * (x >>> 1);
588 }
589
590
591
592
593
594
595 return edgeSample(x);
596 }
597
598
599
600
601
602
603
604
605
606
607 private double edgeSample(long xx) {
608 int j = selectRegion();
609 if (j != 0) {
610
611 return sampleOverhang(j, xx);
612 }
613
614
615
616
617
618
619
620
621 double x0 = X_0;
622 for (;;) {
623
624 final long x = nextLong();
625 final int i = ((int) x) & 0xff;
626
627 if (i < I_MAX) {
628
629 return x0 + X[i] * (x >>> 1);
630 }
631
632
633 j = selectRegion();
634 if (j != 0) {
635 return x0 + sampleOverhang(j, x);
636 }
637
638
639 x0 += X_0;
640 }
641 }
642
643
644
645
646
647
648 private int selectRegion() {
649 final long x = nextLong();
650
651 final int j = ((int) x) & MASK_INT8;
652
653 return x >= IPMF[j] ? MAP[j] & MASK_INT8 : j;
654 }
655
656
657
658
659
660
661
662
663 private double sampleOverhang(int j, long xx) {
664
665
666 long u1 = xx >>> 1;
667 for (;;) {
668
669
670
671
672
673
674
675
676
677
678
679
680 final long u = randomInt63();
681
682
683 final long u2 = u1 < u ? u : u1;
684 u1 = u1 < u ? u1 : u;
685 final double x = interpolate(X, j, u1);
686 if (u2 - u1 >= E_MAX) {
687
688 return x;
689 }
690
691
692
693
694
695
696 if (interpolate(Y, j, u2) <= Math.exp(-x)) {
697 return x;
698 }
699
700
701 u1 = randomInt63();
702 }
703 }
704
705
706 @Override
707 public Exponential withUniformRandomProvider(UniformRandomProvider rng) {
708 return new Exponential(rng);
709 }
710
711
712
713
714
715
716
717 public static Exponential of(UniformRandomProvider rng) {
718 return new Exponential(rng);
719 }
720
721
722
723
724
725
726
727
728
729 public static Exponential of(UniformRandomProvider rng, double mean) {
730 return new ExponentialMean(rng, InternalUtils.requireStrictlyPositive(mean, "mean"));
731 }
732 }
733
734
735
736
737
738
739
740
741
742
743
744
745
746 public static final class NormalizedGaussian extends ZigguratSampler
747 implements NormalizedGaussianSampler, SharedStateContinuousSampler {
748
749
750
751
752
753
754
755
756
757 private static final int I_MAX = 253;
758
759
760 private static final int J_INFLECTION = 204;
761
762
763
764
765 private static final long CONVEX_E_MAX = -2269182951627976004L;
766
767
768 private static final long CONCAVE_E_MAX = 760463704284035184L;
769
770 private static final double X_0 = 3.6360066255009455861;
771
772 private static final double ONE_OVER_X_0 = 1.0 / X_0;
773
774
775
776
777 private static final byte[] MAP = {
778 (byte) 0, (byte) 0, (byte) 239, (byte) 2, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
779 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 253,
780 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
781 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
782 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
783 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
784 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
785 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
786 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
787 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
788 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
789 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
790 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
791 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
792 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
793 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
794 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
795 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
796 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
797 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
798 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
799 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
800 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
801 (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253, (byte) 253,
802 (byte) 253, (byte) 253, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 252,
803 (byte) 252, (byte) 252, (byte) 252, (byte) 252, (byte) 251, (byte) 251, (byte) 251, (byte) 251,
804 (byte) 251, (byte) 251, (byte) 251, (byte) 250, (byte) 250, (byte) 250, (byte) 250, (byte) 250,
805 (byte) 249, (byte) 249, (byte) 249, (byte) 248, (byte) 248, (byte) 248, (byte) 247, (byte) 247,
806 (byte) 247, (byte) 246, (byte) 246, (byte) 245, (byte) 244, (byte) 244, (byte) 243, (byte) 242,
807 (byte) 240, (byte) 2, (byte) 2, (byte) 3, (byte) 3, (byte) 0, (byte) 0, (byte) 240,
808 (byte) 241, (byte) 242, (byte) 243, (byte) 244, (byte) 245, (byte) 246, (byte) 247, (byte) 248,
809 (byte) 249, (byte) 250, (byte) 251, (byte) 252, (byte) 253, (byte) 1, (byte) 0, (byte) 0,
810 };
811
812
813
814
815 private static final long[] IPMF = {
816 9223372036854775296L, 1100243796534090752L, 7866600928998383104L, 6788754710675124736L,
817 9022865200181688320L, 6522434035205502208L, 4723064097360024576L, 3360495653216416000L,
818 2289663232373870848L, 1423968905551920384L, 708364817827798016L, 106102487305601280L,
819 -408333464665794560L, -853239722779025152L, -1242095211825521408L, -1585059631105762048L,
820 -1889943050287169024L, -2162852901990669824L, -2408637386594511104L, -2631196530262954496L,
821 -2833704942520925696L, -3018774289025787392L, -3188573753472222208L, -3344920681707410944L,
822 -3489349705062150656L, -3623166100042179584L, -3747487436868335360L, -3863276422712173824L,
823 -3971367044063130880L, -4072485557029824000L, -4167267476830916608L, -4256271432240159744L,
824 -4339990541927306752L, -4418861817133802240L, -4493273980372377088L, -4563574004462246656L,
825 -4630072609770453760L, -4693048910430964992L, -4752754358862894848L, -4809416110052769536L,
826 -4863239903586985984L, -4914412541515875840L, -4963104028439161088L, -5009469424769119232L,
827 -5053650458856559360L, -5095776932695077632L, -5135967952544929024L, -5174333008451230720L,
828 -5210972924952654336L, -5245980700100460288L, -5279442247516297472L, -5311437055462369280L,
829 -5342038772315650560L, -5371315728843297024L, -5399331404632512768L, -5426144845448965120L,
830 -5451811038519422464L, -5476381248265593088L, -5499903320558339072L, -5522421955752311296L,
831 -5543978956085263616L, -5564613449659060480L, -5584362093436146432L, -5603259257517428736L,
832 -5621337193070986240L, -5638626184974132224L, -5655154691220933888L, -5670949470294763008L,
833 -5686035697601807872L, -5700437072199152384L, -5714175914219812352L, -5727273255295220992L,
834 -5739748920271997440L, -5751621603810412032L, -5762908939773946112L, -5773627565915007744L,
835 -5783793183152377600L, -5793420610475628544L, -5802523835894661376L, -5811116062947570176L,
836 -5819209754516120832L, -5826816672854571776L, -5833947916825278208L, -5840613956570608128L,
837 -5846824665591763456L, -5852589350491075328L, -5857916778480726528L, -5862815203334800384L,
838 -5867292388935742464L, -5871355631762284032L, -5875011781262890752L, -5878267259039093760L,
839 -5881128076579883520L, -5883599852028851456L, -5885687825288565248L, -5887396872144963840L,
840 -5888731517955042304L, -5889695949247728384L, -5890294025706689792L, -5890529289910829568L,
841 -5890404977675987456L, -5889924026487208448L, -5889089083913555968L, -5887902514965209344L,
842 -5886366408898372096L, -5884482585690639872L, -5882252601321090304L, -5879677752995027712L,
843 -5876759083794175232L, -5873497386318840832L, -5869893206505510144L, -5865946846617024256L,
844 -5861658367354159104L, -5857027590486131456L, -5852054100063428352L, -5846737243971504640L,
845 -5841076134082373632L, -5835069647234580480L, -5828716424754549248L, -5822014871949021952L,
846 -5814963157357531648L, -5807559211080072192L, -5799800723447229952L, -5791685142338073344L,
847 -5783209670985158912L, -5774371264582489344L, -5765166627072226560L, -5755592207057667840L,
848 -5745644193442049280L, -5735318510777133824L, -5724610813433666560L, -5713516480340333056L,
849 -5702030608556698112L, -5690148005851018752L, -5677863184109371904L, -5665170350903313408L,
850 -5652063400924580608L, -5638535907000141312L, -5624581109999480320L, -5610191908627599872L,
851 -5595360848093632768L, -5580080108034218752L, -5564341489875549952L, -5548136403221394688L,
852 -5531455851545399296L, -5514290416593586944L, -5496630242226406656L, -5478465016761742848L,
853 -5459783954986665216L, -5440575777891777024L, -5420828692432397824L, -5400530368638773504L,
854 -5379667916699401728L, -5358227861294116864L, -5336196115274292224L, -5313557951078385920L,
855 -5290297970633451520L, -5266400072915222272L, -5241847420214015744L, -5216622401043726592L,
856 -5190706591719534080L, -5164080714589203200L, -5136724594099067136L, -5108617109269313024L,
857 -5079736143458214912L, -5050058530461741312L, -5019559997031891968L, -4988215100963582976L,
858 -4955997165645491968L, -4922878208652041728L, -4888828866780320000L, -4853818314258475776L,
859 -4817814175855180032L, -4780782432601701888L, -4742687321746719232L, -4703491227581444608L,
860 -4663154564978699264L, -4621635653358766336L, -4578890580370785792L, -4534873055659683584L,
861 -4489534251700611840L, -4442822631898829568L, -4394683764809104128L, -4345060121983362560L,
862 -4293890858708922880L, -4241111576153830144L, -4186654061692619008L, -4130446006804747776L,
863 -4072410698657718784L, -4012466683838401024L, -3950527400305017856L, -3886500774061896704L,
864 -3820288777467837184L, -3751786943594897664L, -3680883832433527808L, -3607460442623922176L,
865 -3531389562483324160L, -3452535052891361792L, -3370751053395887872L, -3285881101633968128L,
866 -3197757155301365504L, -3106198503156485376L, -3011010550911937280L, -2911983463883580928L,
867 -2808890647470271744L, -2701487041141149952L, -2589507199690603520L, -2472663129329160192L,
868 -2350641842139870464L, -2223102583770035200L, -2089673683684728576L, -1949948966090106880L,
869 -1803483646855993856L, -1649789631480328192L, -1488330106139747584L, -1318513295725618176L,
870 -1139685236927327232L, -951121376596854784L, -752016768184775936L, -541474585642866432L,
871 -318492605725778432L, -81947227249193216L, 169425512612864512L, 437052607232193536L,
872 722551297568809984L, 1027761939299714304L, 1354787941622770432L, 1706044619203941632L,
873 2084319374409574144L, 2492846399593711360L, 2935400169348532480L, 3416413484613111552L,
874 3941127949860576256L, 4515787798793437952L, 5147892401439714304L, 5846529325380406016L,
875 6622819682216655360L, 7490522659874166016L, 8466869998277892096L, 8216968526387345408L,
876 4550693915488934656L, 7628019504138977280L, 6605080500908005888L, 7121156327650272512L,
877 2484871780331574272L, 7179104797032803328L, 7066086283830045440L, 1516500120817362944L,
878 216305945438803456L, 6295963418525324544L, 2889316805630113280L, -2712587580533804032L,
879 6562498853538167040L, 7975754821147501312L, -9223372036854775808L, -9223372036854775808L,
880 };
881
882
883
884
885
886
887
888
889
890
891 private static final double[] X = {
892 3.9421662825398133e-19, 3.7204945004119012e-19, 3.5827024480628678e-19, 3.4807476236540249e-19,
893 3.3990177171882136e-19, 3.3303778360340139e-19, 3.270943881761755e-19, 3.21835771324951e-19,
894 3.1710758541840432e-19, 3.1280307407034065e-19, 3.0884520655804019e-19, 3.0517650624107352e-19,
895 3.01752902925846e-19, 2.985398344070532e-19, 2.9550967462801797e-19, 2.9263997988491663e-19,
896 2.8991225869977476e-19, 2.8731108780226291e-19, 2.8482346327101335e-19, 2.8243831535194389e-19,
897 2.8014613964727031e-19, 2.7793871261807797e-19, 2.7580886921411212e-19, 2.7375032698308758e-19,
898 2.7175754543391047e-19, 2.6982561247538484e-19, 2.6795015188771505e-19, 2.6612724730440033e-19,
899 2.6435337927976633e-19, 2.6262537282028438e-19, 2.6094035335224142e-19, 2.5929570954331002e-19,
900 2.5768906173214726e-19, 2.5611823497719608e-19, 2.5458123593393361e-19, 2.5307623292372459e-19,
901 2.51601538677984e-19, 2.5015559533646191e-19, 2.4873696135403158e-19, 2.4734430003079206e-19,
902 2.4597636942892726e-19, 2.446320134791245e-19, 2.4331015411139206e-19, 2.4200978427132955e-19,
903 2.4072996170445879e-19, 2.3946980340903347e-19, 2.3822848067252674e-19, 2.3700521461931801e-19,
904 2.357992722074133e-19, 2.3460996262069972e-19, 2.3343663401054455e-19, 2.322786705467384e-19,
905 2.3113548974303765e-19, 2.3000654002704238e-19, 2.2889129852797606e-19, 2.2778926905921897e-19,
906 2.2669998027527321e-19, 2.2562298398527416e-19, 2.245578536072726e-19, 2.2350418274933911e-19,
907 2.2246158390513294e-19, 2.2142968725296249e-19, 2.2040813954857555e-19, 2.1939660310297601e-19,
908 2.1839475483749618e-19, 2.1740228540916853e-19, 2.1641889840016519e-19, 2.1544430956570613e-19,
909 2.1447824613540345e-19, 2.1352044616350571e-19, 2.1257065792395107e-19, 2.1162863934653125e-19,
910 2.1069415749082026e-19, 2.0976698805483467e-19, 2.0884691491567363e-19, 2.0793372969963634e-19,
911 2.0702723137954107e-19, 2.0612722589717129e-19, 2.0523352580895635e-19, 2.0434594995315797e-19,
912 2.0346432313698148e-19, 2.0258847584216418e-19, 2.0171824394771313e-19, 2.0085346846857531e-19,
913 1.9999399530912015e-19, 1.9913967503040585e-19, 1.9829036263028144e-19, 1.9744591733545175e-19,
914 1.9660620240469857e-19, 1.9577108494251485e-19, 1.9494043572246307e-19, 1.9411412901962161e-19,
915 1.9329204245152935e-19, 1.9247405682708168e-19, 1.9166005600287074e-19, 1.9084992674649826e-19,
916 1.900435586064234e-19, 1.8924084378793725e-19, 1.8844167703488436e-19, 1.8764595551677749e-19,
917 1.868535787209745e-19, 1.8606444834960934e-19, 1.8527846822098793e-19, 1.8449554417517928e-19,
918 1.8371558398354868e-19, 1.8293849726199566e-19, 1.8216419538767393e-19, 1.8139259141898448e-19,
919 1.8062360001864453e-19, 1.7985713737964743e-19, 1.7909312115393845e-19, 1.78331470383642e-19,
920 1.7757210543468428e-19, 1.7681494793266395e-19, 1.760599207008314e-19, 1.7530694770004409e-19,
921 1.7455595397057217e-19, 1.7380686557563475e-19, 1.7305960954655264e-19, 1.7231411382940904e-19,
922 1.7157030723311378e-19, 1.7082811937877138e-19, 1.7008748065025788e-19, 1.6934832214591352e-19,
923 1.6861057563126349e-19, 1.6787417349268046e-19, 1.6713904869190636e-19, 1.6640513472135291e-19,
924 1.6567236556010242e-19, 1.6494067563053266e-19, 1.6420999975549115e-19, 1.6348027311594532e-19,
925 1.6275143120903661e-19, 1.6202340980646725e-19, 1.6129614491314931e-19, 1.6056957272604589e-19,
926 1.5984362959313479e-19, 1.5911825197242491e-19, 1.5839337639095554e-19, 1.57668939403708e-19,
927 1.5694487755235889e-19, 1.5622112732380261e-19, 1.554976251083707e-19, 1.5477430715767271e-19,
928 1.540511095419833e-19, 1.5332796810709688e-19, 1.5260481843056974e-19, 1.5188159577726683e-19,
929 1.5115823505412761e-19, 1.5043467076406199e-19, 1.4971083695888395e-19, 1.4898666719118714e-19,
930 1.4826209446506113e-19, 1.4753705118554365e-19, 1.468114691066983e-19, 1.4608527927820112e-19,
931 1.4535841199031451e-19, 1.4463079671711862e-19, 1.4390236205786415e-19, 1.4317303567630177e-19,
932 1.4244274423783481e-19, 1.4171141334433217e-19, 1.4097896746642792e-19, 1.4024532987312287e-19,
933 1.3951042255849034e-19, 1.3877416616527576e-19, 1.3803647990516385e-19, 1.3729728147547174e-19,
934 1.3655648697200824e-19, 1.3581401079782068e-19, 1.3506976556752901e-19, 1.3432366200692418e-19,
935 1.3357560884748263e-19, 1.3282551271542047e-19, 1.3207327801488087e-19, 1.3131880680481524e-19,
936 1.3056199866908076e-19, 1.2980275057923788e-19, 1.2904095674948608e-19, 1.2827650848312727e-19,
937 1.2750929400989213e-19, 1.2673919831340482e-19, 1.2596610294799512e-19, 1.2518988584399374e-19,
938 1.2441042110056523e-19, 1.2362757876504165e-19, 1.2284122459762072e-19, 1.2205121982017852e-19,
939 1.2125742084782245e-19, 1.2045967900166973e-19, 1.196578402011802e-19, 1.1885174463419555e-19,
940 1.1804122640264091e-19, 1.1722611314162064e-19, 1.1640622560939109e-19, 1.1558137724540874e-19,
941 1.1475137369333185e-19, 1.1391601228549047e-19, 1.1307508148492592e-19, 1.1222836028063025e-19,
942 1.1137561753107903e-19, 1.1051661125053526e-19, 1.0965108783189755e-19, 1.0877878119905372e-19,
943 1.0789941188076655e-19, 1.070126859970364e-19, 1.0611829414763286e-19, 1.0521591019102928e-19,
944 1.0430518990027552e-19, 1.0338576948035472e-19, 1.0245726392923699e-19, 1.015192652220931e-19,
945 1.0057134029488235e-19, 9.9613028799672809e-20, 9.8643840599459914e-20, 9.7663252964755816e-20,
946 9.6670707427623454e-20, 9.566560624086667e-20, 9.4647308380433213e-20, 9.3615125017323508e-20,
947 9.2568314370887282e-20, 9.1506075837638774e-20, 9.0427543267725716e-20, 8.933177723376368e-20,
948 8.8217756102327883e-20, 8.7084365674892319e-20, 8.5930387109612162e-20, 8.4754482764244349e-20,
949 8.3555179508462343e-20, 8.2330848933585364e-20, 8.1079683729129853e-20, 7.9799669284133864e-20,
950 7.8488549286072745e-20, 7.7143783700934692e-20, 7.5762496979467566e-20, 7.4341413578485329e-20,
951 7.2876776807378431e-20, 7.1364245443525374e-20, 6.9798760240761066e-20, 6.8174368944799054e-20,
952 6.6483992986198539e-20, 6.4719110345162767e-20, 6.2869314813103699e-20, 6.0921687548281263e-20,
953 5.8859873575576818e-20, 5.6662675116090981e-20, 5.4301813630894571e-20, 5.173817174449422e-20,
954 4.8915031722398545e-20, 4.5744741890755301e-20, 4.2078802568583416e-20, 3.7625986722404761e-20,
955 3.1628589805881879e-20, 0,
956 };
957
958
959
960
961
962
963
964
965
966
967 private static final double[] Y = {
968 1.4598410796619063e-22, 3.0066613427942797e-22, 4.6129728815103466e-22, 6.2663350049234362e-22,
969 7.9594524761881544e-22, 9.6874655021705039e-22, 1.1446877002379439e-21, 1.3235036304379167e-21,
970 1.5049857692053131e-21, 1.6889653000719298e-21, 1.8753025382711626e-21, 2.0638798423695191e-21,
971 2.2545966913644708e-21, 2.4473661518801799e-21, 2.6421122727763533e-21, 2.8387681187879908e-21,
972 3.0372742567457284e-21, 3.2375775699986589e-21, 3.439630315794878e-21, 3.6433893657997798e-21,
973 3.8488155868912312e-21, 4.0558733309492775e-21, 4.264530010428359e-21, 4.4747557422305067e-21,
974 4.6865230465355582e-21, 4.8998065902775257e-21, 5.1145829672105489e-21, 5.3308305082046173e-21,
975 5.5485291167031758e-21, 5.7676601252690476e-21, 5.9882061699178461e-21, 6.2101510795442221e-21,
976 6.4334797782257209e-21, 6.6581781985713897e-21, 6.8842332045893181e-21, 7.1116325227957095e-21,
977 7.3403646804903092e-21, 7.5704189502886418e-21, 7.8017853001379744e-21, 8.0344543481570017e-21,
978 8.2684173217333118e-21, 8.5036660203915022e-21, 8.7401927820109521e-21, 8.9779904520281901e-21,
979 9.2170523553061439e-21, 9.457372270392882e-21, 9.698944405926943e-21, 9.9417633789758424e-21,
980 1.0185824195119818e-20, 1.043112223011477e-20, 1.0677653212987396e-20, 1.0925413210432004e-20,
981 1.1174398612392891e-20, 1.1424606118728715e-20, 1.1676032726866302e-20, 1.1928675720361027e-20,
982 1.2182532658289373e-20, 1.2437601365406785e-20, 1.2693879923010674e-20, 1.2951366660454145e-20,
983 1.3210060147261461e-20, 1.3469959185800733e-20, 1.3731062804473644e-20, 1.3993370251385596e-20,
984 1.4256880988463136e-20, 1.4521594685988369e-20, 1.4787511217522902e-20, 1.505463065519617e-20,
985 1.5322953265335218e-20, 1.5592479504415048e-20, 1.5863210015310328e-20, 1.6135145623830982e-20,
986 1.6408287335525592e-20, 1.6682636332737932e-20, 1.6958193971903124e-20, 1.7234961781071113e-20,
987 1.7512941457646084e-20, 1.7792134866331487e-20, 1.807254403727107e-20, 1.8354171164377277e-20,
988 1.8637018603838945e-20, 1.8921088872801004e-20, 1.9206384648209468e-20, 1.9492908765815636e-20,
989 1.9780664219333857e-20, 2.0069654159747839e-20, 2.0359881894760859e-20, 2.0651350888385696e-20,
990 2.0944064760670539e-20, 2.1238027287557466e-20, 2.1533242400870487e-20, 2.1829714188430474e-20,
991 2.2127446894294597e-20, 2.242644491911827e-20, 2.2726712820637798e-20, 2.3028255314272276e-20,
992 2.3331077273843558e-20, 2.3635183732413286e-20, 2.3940579883236352e-20, 2.4247271080830277e-20,
993 2.455526284216033e-20, 2.4864560847940368e-20, 2.5175170944049622e-20, 2.5487099143065929e-20,
994 2.5800351625915997e-20, 2.6114934743643687e-20, 2.6430855019297323e-20, 2.6748119149937411e-20,
995 2.7066734008766247e-20, 2.7386706647381193e-20, 2.7708044298153558e-20, 2.8030754376735269e-20,
996 2.8354844484695747e-20, 2.8680322412291631e-20, 2.9007196141372126e-20, 2.9335473848423219e-20,
997 2.9665163907753988e-20, 2.9996274894828624e-20, 3.0328815589748056e-20, 3.0662794980885287e-20,
998 3.099822226867876e-20, 3.1335106869588609e-20, 3.1673458420220558e-20, 3.2013286781622988e-20,
999 3.2354602043762612e-20, 3.2697414530184806e-20, 3.304173480286495e-20, 3.3387573667257349e-20,
1000 3.3734942177548938e-20, 3.4083851642125208e-20, 3.4434313629256243e-20, 3.4786339973011376e-20,
1001 3.5139942779411164e-20, 3.5495134432826171e-20, 3.585192760263246e-20, 3.6210335250134172e-20,
1002 3.6570370635764384e-20, 3.6932047326575882e-20, 3.7295379204034252e-20, 3.7660380472126401e-20,
1003 3.8027065665798284e-20, 3.8395449659736649e-20, 3.8765547677510167e-20, 3.9137375301086406e-20,
1004 3.9510948480742172e-20, 3.988628354538543e-20, 4.0263397213308566e-20, 4.0642306603393541e-20,
1005 4.1023029246790967e-20, 4.1405583099096438e-20, 4.1789986553048817e-20, 4.2176258451776819e-20,
1006 4.2564418102621759e-20, 4.2954485291566197e-20, 4.3346480298300118e-20, 4.3740423911958146e-20,
1007 4.4136337447563716e-20, 4.4534242763218286e-20, 4.4934162278076256e-20, 4.5336118991149025e-20,
1008 4.5740136500984466e-20, 4.6146239026271279e-20, 4.6554451427421133e-20, 4.6964799229185088e-20,
1009 4.7377308644364938e-20, 4.7792006598684169e-20, 4.8208920756888113e-20, 4.8628079550147814e-20,
1010 4.9049512204847653e-20, 4.9473248772842596e-20, 4.9899320163277674e-20, 5.0327758176068971e-20,
1011 5.0758595537153414e-20, 5.1191865935622696e-20, 5.1627604062866059e-20, 5.2065845653856416e-20,
1012 5.2506627530725194e-20, 5.2949987648783448e-20, 5.3395965145159426e-20, 5.3844600390237576e-20,
1013 5.4295935042099358e-20, 5.4750012104183868e-20, 5.5206875986405073e-20, 5.5666572569983821e-20,
1014 5.6129149276275792e-20, 5.6594655139902476e-20, 5.7063140886520563e-20, 5.7534659015596918e-20,
1015 5.8009263888591218e-20, 5.8487011822987583e-20, 5.8967961192659803e-20, 5.9452172535103471e-20,
1016 5.9939708666122605e-20, 6.0430634802618929e-20, 6.0925018694200531e-20, 6.142293076440286e-20,
1017 6.1924444262401531e-20, 6.2429635426193939e-20, 6.2938583658336214e-20, 6.3451371715447563e-20,
1018 6.3968085912834963e-20, 6.4488816345752736e-20, 6.5013657128995346e-20, 6.5542706656731714e-20,
1019 6.6076067884730717e-20, 6.6613848637404196e-20, 6.715616194241298e-20, 6.770312639595058e-20,
1020 6.8254866562246408e-20, 6.8811513411327825e-20, 6.9373204799659681e-20, 6.9940085998959109e-20,
1021 7.0512310279279503e-20, 7.1090039553397167e-20, 7.1673445090644796e-20, 7.2262708309655784e-20,
1022 7.2858021661057338e-20, 7.34595896130358e-20, 7.4067629754967553e-20, 7.4682374037052817e-20,
1023 7.5304070167226666e-20, 7.5932983190698547e-20, 7.6569397282483754e-20, 7.7213617789487678e-20,
1024 7.7865973566417016e-20, 7.8526819659456755e-20, 7.919654040385056e-20, 7.9875553017037968e-20,
1025 8.056431178890163e-20, 8.1263312996426176e-20, 8.1973100703706304e-20, 8.2694273652634034e-20,
1026 8.3427493508836792e-20, 8.4173494807453416e-20, 8.4933097052832066e-20, 8.5707219578230905e-20,
1027 8.6496899985930695e-20, 8.7303317295655327e-20, 8.8127821378859504e-20, 8.8971970928196666e-20,
1028 8.9837583239314064e-20, 9.0726800697869543e-20, 9.1642181484063544e-20, 9.2586826406702765e-20,
1029 9.3564561480278864e-20, 9.4580210012636175e-20, 9.5640015550850358e-20, 9.675233477050313e-20,
1030 9.7928851697808831e-20, 9.9186905857531331e-20, 1.0055456271343397e-19, 1.0208407377305566e-19,
1031 1.0390360993240711e-19, 1.0842021724855044e-19,
1032 };
1033
1034
1035 private final SharedStateContinuousSampler exponential;
1036
1037
1038
1039
1040 private NormalizedGaussian(UniformRandomProvider rng) {
1041 super(rng);
1042 exponential = ZigguratSampler.Exponential.of(rng);
1043 }
1044
1045
1046 @Override
1047 public String toString() {
1048 return toString("normalized Gaussian");
1049 }
1050
1051
1052 @Override
1053 public double sample() {
1054
1055
1056 final long xx = nextLong();
1057
1058 final int i = ((int) xx) & MASK_INT8;
1059
1060 if (i < I_MAX) {
1061
1062
1063 return X[i] * xx;
1064 }
1065
1066 return edgeSample(xx);
1067 }
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078 private double edgeSample(long xx) {
1079
1080
1081
1082 long u1 = xx & MAX_INT64;
1083
1084
1085 final double signBit = ((xx >>> 62) & 0x2) - 1.0;
1086 final int j = selectRegion();
1087
1088
1089
1090
1091
1092
1093
1094 double x;
1095 if (j > J_INFLECTION) {
1096
1097
1098
1099 for (;;) {
1100 x = interpolate(X, j, u1);
1101
1102 final long uDistance = randomInt63() - u1;
1103 if (uDistance >= 0) {
1104
1105 break;
1106 }
1107 if (uDistance >= CONVEX_E_MAX &&
1108
1109
1110
1111 interpolate(Y, j, u1 + uDistance) < Math.exp(-0.5 * x * x)) {
1112 break;
1113 }
1114
1115 u1 = randomInt63();
1116 }
1117 } else if (j < J_INFLECTION) {
1118 if (j == 0) {
1119
1120
1121
1122
1123
1124 do {
1125 x = ONE_OVER_X_0 * exponential.sample();
1126 } while (exponential.sample() < 0.5 * x * x);
1127 x += X_0;
1128 } else {
1129
1130
1131
1132 for (;;) {
1133
1134 final long u = randomInt63();
1135
1136
1137 final long u2 = u1 < u ? u : u1;
1138 u1 = u1 < u ? u1 : u;
1139 x = interpolate(X, j, u1);
1140 if (u2 - u1 > CONCAVE_E_MAX ||
1141 interpolate(Y, j, u2) < Math.exp(-0.5 * x * x)) {
1142 break;
1143 }
1144 u1 = randomInt63();
1145 }
1146 }
1147 } else {
1148
1149
1150
1151 for (;;) {
1152 x = interpolate(X, j, u1);
1153 if (interpolate(Y, j, randomInt63()) < Math.exp(-0.5 * x * x)) {
1154 break;
1155 }
1156 u1 = randomInt63();
1157 }
1158 }
1159 return signBit * x;
1160 }
1161
1162
1163
1164
1165
1166
1167 private int selectRegion() {
1168 final long x = nextLong();
1169
1170 final int j = ((int) x) & MASK_INT8;
1171
1172 return x >= IPMF[j] ? MAP[j] & MASK_INT8 : j;
1173 }
1174
1175
1176 @Override
1177 public NormalizedGaussian withUniformRandomProvider(UniformRandomProvider rng) {
1178 return new NormalizedGaussian(rng);
1179 }
1180
1181
1182
1183
1184
1185
1186
1187 public static NormalizedGaussian of(UniformRandomProvider rng) {
1188 return new NormalizedGaussian(rng);
1189 }
1190 }
1191
1192
1193
1194
1195 ZigguratSampler(UniformRandomProvider rng) {
1196 this.rng = rng;
1197 }
1198
1199
1200
1201
1202
1203
1204
1205 String toString(String type) {
1206 return "Modified ziggurat " + type + " deviate [" + rng.toString() + "]";
1207 }
1208
1209
1210
1211
1212
1213
1214 long nextLong() {
1215 return rng.nextLong();
1216 }
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228 long randomInt63() {
1229 return rng.nextLong() >>> 1;
1230 }
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263 static double interpolate(double[] v, int j, long u) {
1264
1265
1266
1267
1268
1269
1270
1271
1272 return v[j] * TWO_POW_63 + u * (v[j - 1] - v[j]);
1273 }
1274 }