File: rand-module.c

package info (click to toggle)
slang2 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,588 kB
  • ctags: 10,558
  • sloc: ansic: 95,506; sh: 3,277; makefile: 945; pascal: 143
file content (1467 lines) | stat: -rw-r--r-- 31,854 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
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
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
/* -*- mode: C; mode: fold -*-
Copyright (C) 2007-2014 John E. Davis

This file is part of the S-Lang Library.

The S-Lang Library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The S-Lang Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
*/
#include "config.h"

#include <stdio.h>
#include <math.h>
#include <slang.h>

#ifdef HAVE_PROCESS_H
# include <process.h>			/* for getpid */
#endif

#include <sys/types.h>

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <time.h>

/* The random number generator used here uses a combination of 3 generators.
 * One of the generators was derived from mzran13 published in
 *
 *   G. Marsaglia and A. Zaman, ``Some portable very-long-period
 * 	random number generators'', Computers in Physics, Vol. 8,
 * 	No. 1, Jan/Feb 1994
 *
 * However, the algorithm (mzran13) published there contained a few
 * typos and as written was not very good.  The subtract-with-carry
 * component of mzran13 adopted for the purposes of the generator in
 * this file has a period of 4x10^28.
 *
 * The other two generators are described at
 * <http://www.helsbreth.org/random/rng_combo.html>.  Briefly, one is
 * a simple product generator, r_{k+2} = r_{k+1} r_k, and the other
 * is is a multiply with carry generator.  The period of this
 * combination exceeds 10^18.
 *
 * The total period of the combined generators exceeds 10^46.
 *
 * The generator was tested using dieharder-2.24.4, and the testing
 * indicates that it may perform a bit better than the Mersenne
 * twister mt19937_1999, which is thought to be a high-quality
 * generator.  The speed is about the same.
 *
 * Note: mt19937_1999 is available to S-Lang users via the GSL module.
 */

SLANG_MODULE(rand);

#ifdef PI
# undef PI
#endif
#define PI 3.14159265358979323846264338327950288
#define LOG_SQRT_2PI 0.9189385332046727417803297364

/*{{{ The basic generator */

#define SEED_X	521288629U
#define SEED_Y	362436069U
#define SEED_Z	16163801U
#define SEED_C  1    /* (SEED_Y > SEED_Z) */

#if (SIZEOF_INT == 4)
typedef unsigned int uint32;
# define UINT32_TYPE SLANG_UINT_TYPE
# define PUSH_UINT32 SLang_push_uint
#else
typedef unsigned long uint32
# define UINT32_TYPE SLANG_ULONG_TYPE
# define PUSH_UINT32 SLang_push_ulong
#endif

#define CACHE_SIZE 4		       /* do not change this */

typedef struct
{
   int cache_index;
   uint32 cache[CACHE_SIZE];
   uint32 x, y, z;
   uint32 cx, cy, cz;

   /* Gaussian Random fields */
   int one_available;
   double g2;
}
Rand_Type;

static uint32 generate_uint32_random (Rand_Type *);

#define NUM_SEEDS 3
static void seed_random (Rand_Type *rt, unsigned long seeds[NUM_SEEDS])
{
   int count;
   unsigned long s1 = seeds[0];
   unsigned long s2 = seeds[1];
   unsigned long s3 = seeds[2];

   rt->x = (uint32) (s1 + SEED_X);
   rt->y = (uint32) (s1/2 + SEED_Y);
   rt->z = (uint32) (2*s1+SEED_Z);
   rt->x += (rt->y > rt->z);

   rt->cache_index = CACHE_SIZE;

   rt->cx = s2 * 8 + 3;
   rt->cy = s2 * 2 + 1;
   rt->cz = s3 | 1;
   count = 32;
   while (count)
     {
	count--;
	(void) generate_uint32_random (rt);
     }

   rt->one_available = 0;
   rt->g2 = 0.0;
}

static uint32 generate_uint32_random (Rand_Type *rt)
{
   uint32 s0, s1, s2, s3, r;
   uint32 *cache;

   cache = rt->cache;
   if (rt->cache_index < CACHE_SIZE)
     return cache[rt->cache_index++];

   s1 = rt->x;
   s2 = rt->y;
   s3 = rt->z;

   r = s0 = (s2-s1) - 18*(s2<=s1); s2 += (s2 <= s1);
   rt->x = s1 = (s3-s2) - 18*(s3<=s2); s3 += (s3 <= s2);
   rt->y = s2 = (s0-s3) - 18*(s0<=s3); s0 += (s0 <= s3);
   rt->z = s3 = (s1-s0) - 18*(s1<=s0); s1 += (s1 <= s0);

#define COMBINE(y,x,a,b) y = ((x)+(a)+(b))
   s1 = rt->cx;
   s2 = rt->cy;
   s3 = rt->cz;

   s0 = s1*s2;
   s3 = 30903U*(s3 & 0xFFFFU) + (s3 >> 16);
   COMBINE(r,r,s0,s3);

   s1 = s2*s0;
   s3 = 30903U*(s3 & 0xFFFFU) + (s3 >> 16);
   COMBINE(cache[1],rt->x,s1,s3);

   rt->cx = s2 = s0*s1;
   s3 = 30903U*(s3 & 0xFFFFU) + (s3 >> 16);
   COMBINE(cache[2],rt->y,s2,s3);

   rt->cy = s0 = s1*s2;
   rt->cz = s3 = 30903U*(s3 & 0xFFFFU) + (s3 >> 16);
   COMBINE(cache[3],rt->z,s0,s3);

   rt->cache_index = 1;
   return r;
}

static void free_random (Rand_Type *r)
{
   SLfree ((char *) r);
}

static Rand_Type *create_random (unsigned long seeds[NUM_SEEDS])
{
   Rand_Type *rt;

   if (NULL != (rt = (Rand_Type *) SLmalloc (sizeof (Rand_Type))))
     seed_random (rt, seeds);
   return rt;
}

/*}}}*/

/*{{{ Flat distribution */

static void generate_random_doubles (Rand_Type *rt, VOID_STAR ap,
				     SLuindex_Type num, VOID_STAR parms)
{
   double *a = (double *)ap;
   double *amax = a + num;
   uint32 *cache = rt->cache;

   (void) parms;

   while (a < amax)
     {
	uint32 u;

	if (rt->cache_index < CACHE_SIZE)
	  u = cache[rt->cache_index++];
	else
	  u = generate_uint32_random (rt);

	*a++ = u / 4294967296.0;
     }
}

static void generate_random_open_doubles (Rand_Type *rt, VOID_STAR ap,
					  SLuindex_Type num, VOID_STAR parms)
{
   double *a = (double *)ap;
   double *amax = a + num;
   uint32 *cache = rt->cache;

   (void) parms;

   while (a < amax)
     {
	uint32 u;

	if (rt->cache_index < CACHE_SIZE)
	  u = cache[rt->cache_index++];
	else
	  u = generate_uint32_random (rt);

	if (u == 0)
	  continue;

	*a++ = u / 4294967296.0;
     }
}

static void generate_random_uints (Rand_Type *rt, VOID_STAR ap,
				   SLuindex_Type num, VOID_STAR parms)
{
   uint32 *a = (uint32 *)ap;
   uint32 *amax = a + num;
   uint32 *cache = rt->cache;

   (void) parms;

   while (a < amax)
     {
	if (rt->cache_index < CACHE_SIZE)
	  *a++ = cache[rt->cache_index++];
	else
	  *a++ = generate_uint32_random (rt);
     }
}

/* produces a random number in the open interval (0,1) */
static double open_interval_random (Rand_Type *rt)
{
   uint32 u;

   do
     {
	if (rt->cache_index < CACHE_SIZE)
	  u = rt->cache[rt->cache_index++];
	else
	  u = generate_uint32_random (rt);
     }
   while (u == 0);

   return u / 4294967296.0;
}

static double uniform_random (Rand_Type *rt)
{
   uint32 u;

   if (rt->cache_index < CACHE_SIZE)
     u = rt->cache[rt->cache_index++];
   else
     u = generate_uint32_random (rt);

   return u / 4294967296.0;
}

/*}}}*/

/*{{{ Gaussian Distribution */

static double gaussian_box_muller (Rand_Type *rt)
{
   double s, g1, g2, g;

   if (rt->one_available)
     {
	rt->one_available = 0;
	return rt->g2;
     }

   do
     {
	uint32 u;
	if (rt->cache_index < CACHE_SIZE)
	  u = rt->cache[rt->cache_index++];
	else
	  u = generate_uint32_random (rt);

	g1 = 2.0*(u/4294967296.0) - 1.0;

	if (rt->cache_index < CACHE_SIZE)
	  u = rt->cache[rt->cache_index++];
	else
	  u = generate_uint32_random (rt);
	g2 = 2.0*(u/4294967296.0) - 1.0;
	g = g1*g1 + g2*g2;
     }
   while ((g >= 1.0) || (g == 0.0));

   s = sqrt (-2.0 * log (g) / g);
   rt->g2 = g2 * s;
   rt->one_available = 1;
   return g1*s;
}

static void generate_gaussian_randoms (Rand_Type *rt, VOID_STAR ap,
				       SLuindex_Type num, VOID_STAR parms)
{
   double *a = (double *)ap;
   double *amax = a + num;
   double sigma = *(double *)parms;

   if ((a < amax) && (rt->one_available))
     {
	*a++ = sigma*rt->g2;
	rt->one_available = 0;
     }

   while (a < amax)
     {
	*a++ = sigma*gaussian_box_muller (rt);
	if (a == amax)
	  break;

	*a++ = sigma*rt->g2;
	rt->one_available = 0;
     }
}

/*}}}*/

/*{{{ Poisson Distribution */

/*
 * The algorithm used is described in:
 * W. Hörmann "The Transformed Rejection Method for Generating
 * Poisson Random Variables", which is available from
 * <http://statistik.wu-wien.ac.at/papers/92-04-13.wh.ps.gz>.
 */
#define FACTORIAL_TABLE_SIZE 10
static double Log_Factorial_Table [FACTORIAL_TABLE_SIZE+1];

static void init_poisson (void)
{
   unsigned int i;
   double x = 1.0;
   Log_Factorial_Table[0] = 0.0;
   for (i = 1; i <= FACTORIAL_TABLE_SIZE; i++)
     {
	x *= i;
	Log_Factorial_Table[i] = log(x);
     }
}

/* Assumes x >= 0 */
static double log_factorial (double x)
{
   double x2;

   if (x <= FACTORIAL_TABLE_SIZE)
     return Log_Factorial_Table[(unsigned int) x];

   x2 = x*x;

   return LOG_SQRT_2PI + (x+0.5)*log(x) - x
     + (13860.0 - (462.0 - (132.0 - (99.0 - 140.0/x2)/x2)/x2)/x2)/x/166320.0;
}

static unsigned int hoermann_ptrd_poisson
  (Rand_Type *rt, double mu, double a, double b, double vr,
      double alphainv, double lnmu, double smu)
{
   while (1)
     {
	double u, v, fk, us;
	unsigned int k;

	v = open_interval_random (rt);
	if (v <= 0.86*vr)
	  {
	     u = v/vr - 0.43;
	     fk = floor((2.0*a/(0.5-fabs(u))+b)*u + mu + 0.445);
	     return (unsigned int)fk;
	  }
	if (v >= vr)
	  u = open_interval_random (rt) - 0.5;
	else
	  {
	     u = v/vr - 0.93; u = ((u < 0.0) ? -0.5 : 0.5) - u;
	     v = vr*open_interval_random (rt);
	  }
	us = 0.5 - fabs(u);
	if ((us < 0.013) && (v > us))
	  continue;

	fk = floor((2.0*a/us + b)*u + mu + 0.445);
	if (fk < 0.0)
	  continue;

	k = (unsigned int) fk;

	v = v * alphainv/(a/(us*us) + b);
	if ((k >= 10)
	    && (log(v*smu) <= (fk+0.5)*log(mu/fk)-mu-LOG_SQRT_2PI+fk
		- (1.0/12-1.0/(360.0*fk*fk))/fk))
	  return (unsigned int)fk;

	if ((k <= 9)
	    && (log(v)<=fk*lnmu-mu-Log_Factorial_Table[k]))
	  return k;
     }
}

static unsigned int knuth_poisson (Rand_Type *rt, double cutoff)
{
   double x = 1.0;
   unsigned int n = 0;

   do
     {
	if (rt->cache_index < CACHE_SIZE)
	  x *= rt->cache[rt->cache_index++] / 4294967296.0;
	else
	  x *= generate_uint32_random (rt) / 4294967296.0;
	n++;
     }
   while (x >= cutoff);

   return n - 1;
}

static void generate_poisson_randoms (Rand_Type *rt, VOID_STAR ap,
				      SLuindex_Type num, VOID_STAR parms)
{
   unsigned int *x = (unsigned int *)ap;
   unsigned int *xmax = x + num;
   double mu = *(double *)parms;
   double cutoff;

   if (mu > 10.0)
     {
	double smu = sqrt(mu);
	double b = 0.931 + 2.53*smu;
	double a = -0.059 + 0.02483*b;
	double vr = 0.9277 - 3.6224/(b-2.0);
	double alphainv = 1.1239 + 1.1328/(b-3.4);
	double lnmu = log(mu);

	while (x < xmax)
	  {
	     *x++ = hoermann_ptrd_poisson (rt, mu, a, b, vr, alphainv, lnmu, smu);
	  }
	return;
     }

   cutoff = exp(-mu);
   while (x < xmax)
     {
	*x++ = knuth_poisson (rt, cutoff);
     }
}

/*}}}*/

/*{{{ Gamma Distribution */

/* The gamma distribution is:
 *
 *  f(x; k,theta)dx = x^{k-1} \frac{e^{-x/theta}}{\theta^k \Gamma(k)} dx
 *    where x>0, k,theta>0.
 * The marsaglia_tsang_gamma algorithm is for x>0, k>=1, theta=1:
 *  f(y;k) = y^{k-1}\frac{e^{-y}}{\Gamma(k)}
 * Let x = theta*y.  dy = dx/theta
 *
 * Then:
 *
 *  f(y;k)dy = (x/theta)^{k-1}\frac{e^{-x/theta}}{\Gamma(k)} dx / \theta
 *   = x^{k-1}\frac{e^{-x/theta}}{\theta^k\Gamma(k)} dx
 *   = f(x; k,theta)
 *
 * Also the final note in the Marsaglia paper says that values for
 * k<1 may be obtained using X_k = X_{k+1}U^{1/k} where X is a random
 * value produced by the algorithm, and U is a uniform random number
 * on (0,1).
 *
 * Marsaglia, G. and Tsang, W. W. 2000a. A simple method for generating
 *  gamma variables.  ACM Transactions on Mathematical Software 26, 363–372.
 * <http://oldmill.uchicago.edu/~wilder/Code/random/Papers/Marsaglia_00_SMGGV.pdf>
 *
 */
static double marsaglia_tsang_gamma_internal (Rand_Type *rt, double c, double d)
{
   double v, u;

   while (1)
     {
	double x;
        do
          {
	     if (rt->one_available)
	       {
		  x = rt->g2;
		  rt->one_available = 0;
	       }
	     else x = gaussian_box_muller (rt);
	     v = 1.0 + c*x;
          }
        while (v <= 0.0);

        v = v*v*v;
        u = open_interval_random (rt);
	x = x*x;

        if ((u < 1.0 - 0.0331*(x*x))
	    || (log (u) < 0.5*x + d*(1.0 - v + log (v))))
          return d*v;
      }
}

/* k > 0. theta > 0 */
static double rand_gamma (Rand_Type *rt, double k, double theta)
{
   double c, d;

#ifdef HAVE_ISNAN
   if (isnan(k) || isnan(theta))
     return k*theta;
#endif

   if (k < 1.0)
     {
	d = k + 2.0/3.0;
	c = (1.0/3.0)/sqrt(d);
	return theta * marsaglia_tsang_gamma_internal (rt, c, d)
	  * pow (open_interval_random(rt), 1.0/k);
     }

   d = k - 1.0/3.0;
   c = (1.0/3.0)/sqrt(d);
   return theta * marsaglia_tsang_gamma_internal (rt, c, d);
}

static void generate_gamma_randoms (Rand_Type *rt, VOID_STAR ap,
				    SLuindex_Type num, VOID_STAR parms)
{
   double *x = (double *)ap;
   double *xmax = x + num;
   double k, theta;
   double c, d;

   k = ((double *)parms)[0];
   theta = ((double *)parms)[1];

#ifdef HAVE_ISNAN
   if (isnan(k) || isnan(theta))
     {
	while (x < xmax)
	  *x++ = k*theta;
	return;
     }
#endif

   if (k < 1.0)
     {
	double kinv = 1.0/k;
	d = k + 2.0/3.0;
	c = (1.0/3.0)/sqrt(d);

	while (x < xmax)
	  {
	     *x++ = theta * marsaglia_tsang_gamma_internal (rt, c, d)
	       * pow (open_interval_random(rt), kinv);
	  }
	return;
     }

   d = k - 1.0/3.0;
   c = (1.0/3.0)/sqrt(d);
   while (x < xmax)
     *x++ = theta * marsaglia_tsang_gamma_internal (rt, c, d);
}

/*}}}*/

/*{{{ Beta Distribution */

static double knuth_beta (Rand_Type *rt, double alpha, double beta)
{
   if (0.0 == (alpha = rand_gamma (rt, alpha, 1.0)))
     return 0.0;

   beta = rand_gamma (rt, beta, 1.0);

   return alpha/(alpha+beta);
}

static void generate_beta_randoms (Rand_Type *rt, VOID_STAR ap,
				   SLuindex_Type num, VOID_STAR parms)
{
   double *x = (double *)ap;
   double *xmax = x + num;
   double alpha, beta;

   alpha = ((double *)parms)[0];
   beta = ((double *)parms)[1];

   while (x < xmax)
     *x++ = knuth_beta (rt, alpha, beta);
}

/*}}}*/

/*{{{ Binomial Distribution */

/* This algorithm is from:
 *  Hormann, W. (1993), The generation of binomial random variates,
 *    Journal of Statistical Computation and Simulation 46, 101-110.
 * I obtained it from the preprint, but not the actual journal article.
 * The preprint contains a typo in the algorithm that I managed to
 * find and correct.  It pays to read the paper.
 */

typedef struct
{
   double a, b, c, vr, alpha, lpq, fm, h, p;
   unsigned int n;
}
BTRS_Type;

static void init_btrs (BTRS_Type *btrs, double p, unsigned int n)
{
   double spq = sqrt (n*p*(1.0-p));

   btrs->p = p;
   btrs->n = n;

   btrs->b = 1.15 + 2.53*spq;
   btrs->a = -0.0873 + 0.0248*btrs->b + 0.01*p;
   btrs->c = n*p+0.5;
   btrs->vr = 0.92 - 4.2/btrs->b;
   btrs->alpha = (2.83+5.1/btrs->b) * spq;
   btrs->lpq = log (p/(1.0-p));
   btrs->fm = floor ((n+1)*p);
   btrs->h = log_factorial (btrs->fm) + log_factorial(n-btrs->fm);
}

/* This algorithm assumes that p <= 0.5 and p*n >= 10.0 */
static double binomial_btrs (Rand_Type *rt, BTRS_Type *btrs)
{
   double a = btrs->a;
   double b = btrs->b;
   double c = btrs->c;
   double vr = btrs->vr;
   double h = btrs->h;
   double lpq = btrs->lpq;
   double fm = btrs->fm;
   double alpha = btrs->alpha;
   unsigned int n = btrs->n;

   while (1)
     {
	double u = open_interval_random (rt) - 0.5;
	double v = open_interval_random (rt);
	double us = 0.5 - fabs(u);
	double fk = floor ((2.0*a/us + b)*u + c);

	if ((fk < 0.0) || ((unsigned int) fk > n))
	  continue;

	if ((us >= 0.07) && (v <= vr))
	  return (unsigned int) fk;

	v = log (v*alpha/(a/(us*us) + b));
	if (v <= (h - log_factorial(fk) - log_factorial(n-fk)
		  + (fk-fm)*lpq))
	  return (unsigned int) fk;
     }
}

typedef struct
{
   unsigned int n;
   double p;
}
Binomial_Parms_Type;

static void generate_binomial_randoms (Rand_Type *rt, VOID_STAR ap,
				       SLuindex_Type num, VOID_STAR parms)
{
   unsigned int *x = (unsigned int *)ap;
   unsigned *xmax = x + num;
   Binomial_Parms_Type *s = (Binomial_Parms_Type *)parms;
   double p, qn, r, q, g;
   unsigned int n;
   int swapped = 0;

   n = s->n;
   p = s->p;

   if (p > 0.5)
     {
	p = 1.0 - p;
	swapped = 1;
     }

   if (n*p > 10.0)
     {
	BTRS_Type btrs;
	init_btrs (&btrs, p, n);
	if (swapped)
	  {
	     while (x < xmax)
	       *x++ = n - binomial_btrs (rt, &btrs);
	  }
	else
	  {
	     while (x < xmax)
	       *x++ = binomial_btrs (rt, &btrs);
	  }
	return;
     }

   /* Inverse CDF method (Adapted from RANLIB) */
   q = 1.0 - p;
   qn = pow (q, n);
   r = p/q;
   g = r*(n+1);
   while (x < xmax)
     {
#define MAX_INV_BINOMIAL_CDF_LOOPS 110
	double f = qn;
	double u = uniform_random (rt);
	unsigned int k = 0;
	unsigned kmax = (n > MAX_INV_BINOMIAL_CDF_LOOPS
			 ? MAX_INV_BINOMIAL_CDF_LOOPS : n);

	while (k <= kmax)
	  {
	     if (u < f)
	       {
		  if (swapped)
		    k = n - k;
		  *x++ = k;
		  break;
	       }
	     u -= f;
	     k++;
	     f *= (g/k - r);
	  }
     }
}

/*}}}*/

static void generate_cauchy_randoms (Rand_Type *rt, VOID_STAR ap,
				     SLuindex_Type num, VOID_STAR parms)
{
   double *x = (double *)ap;
   double *xmax = x + num;
   double a = *(double *)parms;

   while (x < xmax)
     {
	double u;
	do
	  {
	     u = uniform_random (rt);
	  }
	while (u == 0.5);
	*x++ = a * tan (PI*u);
     }
}

static void generate_geometric_randoms (Rand_Type *rt, VOID_STAR ap,
					SLuindex_Type num, VOID_STAR parms)
{
   unsigned int *x = (unsigned int *)ap;
   unsigned int *xmax = x + num;
   double a = *(double *)parms;

   if (a == 1.0)
     {
	while (x < xmax)
	  *x++ = 1;
	return;
     }

   a = 1.0 / log (1.0-a);	       /* is negative */

   while (x < xmax)
     *x++ = (unsigned int) (1.0 + a*log(open_interval_random (rt)));
}

/* Interpreter Interface */

static Rand_Type *Default_Rand;
static int Rand_Type_Id = -1;

static int pop_seeds (unsigned long seeds[NUM_SEEDS])
{
   SLang_Array_Type *at;
   unsigned long *s;
   SLuindex_Type i;

   if (-1 == SLang_pop_array_of_type (&at, SLANG_ULONG_TYPE))
     return -1;

   if (at->num_elements == 0)
     {
	SLang_verror (SL_InvalidParm_Error, "The seed array has no elements");
	SLang_free_array (at);
	return -1;
     }

   s = (unsigned long *)at->data;
   i = 0;
   while (i < NUM_SEEDS)
     {
	seeds[i] = *s;
	i++;
	if (i < at->num_elements)
	  s++;
     }
   SLang_free_array (at);
   return 0;
}

static void generate_seeds (unsigned long seeds[NUM_SEEDS])
{
   unsigned int i;
   unsigned long s = (unsigned long) time(NULL)*(unsigned long) getpid ();
   for (i = 0; i < NUM_SEEDS; i++)
     {
	s = s*69069UL + 1013904243UL;
	seeds[i] = s;
     }
}

static void new_rand_intrin (void) /*{{{*/
{
   unsigned long seeds[NUM_SEEDS];
   Rand_Type *r;
   SLang_MMT_Type *mmt;

   if (SLang_Num_Function_Args == 1)
     {
	if (-1 == pop_seeds (seeds))
	  return;
     }
   else generate_seeds (seeds);

   if (NULL == (r = create_random (seeds)))
     return;

   if (NULL == (mmt = SLang_create_mmt (Rand_Type_Id, (VOID_STAR) r)))
     {
	free_random (r);
	return;
     }

   if (0 == SLang_push_mmt (mmt))
     return;

   SLang_free_mmt (mmt);
}

/*}}}*/

/*{{{ Utility Functions */

static void destroy_rand_type (SLtype type, VOID_STAR vr)
{
   (void) type;
   free_random ((Rand_Type *) vr);
}

static int pop_rand_type_and_dims (int argc, SLang_MMT_Type **mmtp,
				   SLindex_Type *dims, unsigned int *ndims,
				   int *is_scalarp)
{
   int type;
   SLang_MMT_Type *mmt;
   unsigned int i, imax;

   *mmtp = NULL;

   switch (argc)
     {
      default:
	SLang_verror (SL_NumArgs_Error, "Expecting 0, 1, or 2 arguments");
	return -1;

      case 0:
	*is_scalarp = 1;
	return 0;

      case 1:
	type = SLang_peek_at_stack ();
	if (type == Rand_Type_Id)
	  {
	     if (NULL == (mmt = SLang_pop_mmt (Rand_Type_Id)))
	       return -1;
	     *is_scalarp = 1;
	     *mmtp = mmt;
	     return 0;
 	  }
	break;

      case 2:
	type = SLang_peek_at_stack ();
	break;
     }

   *is_scalarp = 0;

   if (type != SLANG_ARRAY_TYPE)
     {
	if (-1 == SLang_pop_array_index (dims))
	  return -1;

	*ndims = 1;
     }
   else
     {
	SLang_Array_Type *at;
	if (-1 == SLang_pop_array (&at, 1))
	  return -1;

	*ndims = imax = at->num_dims;
	for (i = 0; i < imax; i++)
	  dims[i] = at->dims[i];
	SLang_free_array (at);
     }

   if (argc == 2)
     {
	if (NULL == (mmt = SLang_pop_mmt (Rand_Type_Id)))
	  return -1;

	*mmtp = mmt;
     }
   return 0;
}

static int do_xxxrand (int argc, SLtype type,
		       void (*func)(Rand_Type *, VOID_STAR, SLuindex_Type, VOID_STAR),
		       VOID_STAR parms,
		       int *is_scalar_p, VOID_STAR scalar_addr)
{
   SLang_Array_Type *at;
   SLindex_Type dims [SLARRAY_MAX_DIMS];
   unsigned int ndims;
   SLang_MMT_Type *mmt;
   Rand_Type *rt;
   int is_scalar;
   int status = -1;

   if (-1 == pop_rand_type_and_dims (argc, &mmt, dims, &ndims, &is_scalar))
     return -1;

   if (mmt != NULL)
     {
	if (NULL == (rt = (Rand_Type *) SLang_object_from_mmt (mmt)))
	  goto free_return;
     }
   else
     rt = Default_Rand;

   *is_scalar_p = is_scalar;

   if (is_scalar)
     {
	(*func) (rt, scalar_addr, 1, parms);
	status = 0;
	goto free_return;
     }

   if (NULL == (at = SLang_create_array (type, 0, NULL, dims, ndims)))
     goto free_return;

   (*func) (rt, at->data, at->num_elements, parms);
   status = SLang_push_array (at, 0);
   SLang_free_array (at);
   /* drop */

   free_return:
   if (mmt != NULL)
     SLang_free_mmt (mmt);

   return status;
}

/* The calling syntax for the generators is:
 *   rand_foo ([Rand_Type,] args... [,num]);
 */
static int check_stack_args (int num_args, int num_parms, SLFUTURE_CONST char *usage, int *nargsp)
{
   if ((num_args < num_parms) || (num_args > num_parms + 2))
     goto usage_error;

   *nargsp = num_args - num_parms;

   if ((num_args == num_parms) || (num_parms == 0))
     return 0;			       /* rand_foo (parms...) */

   if (num_args == num_parms + 2)
     {
	if (Rand_Type_Id != SLang_peek_at_stack_n (num_args-1))
	  goto usage_error;

	/* rand_foo (r, parms..., num) */
     }
   else if (Rand_Type_Id == SLang_peek_at_stack_n (num_args-1))
     return 0;		       /* rand_foo (r, parms...) */

   return SLroll_stack (num_parms + 1);

usage_error:
   SLang_verror (SL_Usage_Error, "Usage: %s", usage);
   return -1;
}

/*}}}*/

static void urand_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_uniform ([Rand_Type] [num])";
   int is_scalar;
   int nargs;
   double d;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 0, usage, &nargs))
     return;

   if (-1 == do_xxxrand (nargs, SLANG_DOUBLE_TYPE,
			 generate_random_doubles, NULL, &is_scalar, &d))
     return;

   if (is_scalar)
     (void) SLang_push_double (d);
}

/*}}}*/

static void urand_pos_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_uniform_pos ([Rand_Type] [num])";
   int is_scalar;
   int nargs;
   double d;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 0, usage, &nargs))
     return;

   if (-1 == do_xxxrand (nargs, SLANG_DOUBLE_TYPE,
			 generate_random_open_doubles, NULL, &is_scalar, &d))
     return;

   if (is_scalar)
     (void) SLang_push_double (d);
}

/*}}}*/

static void rand_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand ([Rand_Type] [num])";
   int is_scalar;
   int nargs;
   uint32 u;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 0, usage, &nargs))
     return;

   if (-1 == do_xxxrand (nargs, UINT32_TYPE,
			 generate_random_uints, NULL, &is_scalar, &u))
     return;
   if (is_scalar)
     (void) PUSH_UINT32 (u);
}

/*}}}*/

static void rand_gauss_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_gauss ([Rand_Type,] sigma [,num])";
   int is_scalar;
   double d, sigma;
   int nargs;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 1, usage, &nargs))
     return;

   if (-1 == SLang_pop_double (&sigma))
     return;
   sigma = fabs(sigma);

   if (-1 == do_xxxrand (nargs, SLANG_DOUBLE_TYPE,
			 generate_gaussian_randoms, (VOID_STAR) &sigma,
			 &is_scalar, &d))
     return;
   if (is_scalar)
     (void) SLang_push_double (d);
}

/*}}}*/

static void rand_beta_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_beta ([Rand_Type,] a, b [,num])";
   int is_scalar;
   double d;
   double parms[2];
   int nargs;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 2, usage, &nargs))
     return;

   if ((-1 == SLang_pop_double (parms+1))
       || (-1 == SLang_pop_double (parms)))
     return;

   if ((parms[0] <= 0.0) || (parms[1] <= 0.0))
     {
	SLang_verror (SL_Domain_Error, "rand_beta parameters must be > 0");
	return;
     }

   if (-1 == do_xxxrand (nargs, SLANG_DOUBLE_TYPE,
			 generate_beta_randoms, (VOID_STAR) parms,
			 &is_scalar, &d))
     return;
   if (is_scalar)
     (void) SLang_push_double (d);
}

/*}}}*/

static void rand_cauchy_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_cauchy ([Rand_Type,] gamma, [,num])";
   int is_scalar;
   double d;
   double a;
   int nargs;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 1, usage, &nargs))
     return;

   if (-1 == SLang_pop_double (&a))
     return;

   a = fabs(a);

   if (-1 == do_xxxrand (nargs, SLANG_DOUBLE_TYPE,
			 generate_cauchy_randoms, (VOID_STAR) &a,
			 &is_scalar, &d))
     return;
   if (is_scalar)
     (void) SLang_push_double (d);
}

/*}}}*/

static void rand_geometric_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_geometric ([Rand_Type,] p, [,num])";
   int is_scalar;
   double p;
   unsigned int d;
   int nargs;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 1, usage, &nargs))
     return;

   if (-1 == SLang_pop_double (&p))
     return;

   if ((p < 0.0) || (p > 1.0))
     {
	SLang_verror (SL_Domain_Error, "rand_geometric parameter must be beteen 0 and 1");
	return;
     }

   if (-1 == do_xxxrand (nargs, SLANG_UINT_TYPE,
			 generate_geometric_randoms, (VOID_STAR) &p,
			 &is_scalar, &d))
     return;
   if (is_scalar)
     (void) SLang_push_uint (d);
}

/*}}}*/

static void rand_poisson_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_poisson ([Rand_Type,] mu [,num])";
   unsigned int p;
   int is_scalar;
   int nargs;
   double mu;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 1, usage, &nargs))
     return;

   if (-1 == SLang_pop_double (&mu))
     return;

   if (mu < 0.0)
     SLang_verror (SL_InvalidParm_Error, "The poisson rate must be non-negative");

   if (-1 == do_xxxrand (nargs, SLANG_UINT_TYPE,
			 generate_poisson_randoms, (VOID_STAR) &mu,
			 &is_scalar, &p))
     return;

   if (is_scalar)
     (void) SLang_push_uint (p);
}

/*}}}*/

static void rand_gamma_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_gamma([Rand_Type,] k, theta [,num])";
   int is_scalar, nargs;
   double parms[2];
   double p, k, theta;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 2, usage, &nargs))
     return;

   if ((-1 == SLang_pop_double (&theta))
       || (-1 == SLang_pop_double (&k)))
     return;

   if ((theta <= 0) || (k <= 0))
     {
	SLang_verror (SL_InvalidParm_Error, "rand_gamma assumes k,theta>0");
	return;
     }
   parms[0] = k;
   parms[1] = theta;

   if (-1 == do_xxxrand (nargs, SLANG_DOUBLE_TYPE,
			 generate_gamma_randoms, (VOID_STAR) parms,
			 &is_scalar, &p))
     return;

   if (is_scalar)
     (void) SLang_push_double (p);
}

/*}}}*/

static void rand_binomial_intrin (void) /*{{{*/
{
   SLFUTURE_CONST char *usage = "r = rand_binomial ([Rand_Type,] p, n [,num])";
   int is_scalar, nargs;
   Binomial_Parms_Type bp;
   unsigned int u;
   int n;

   if (-1 == check_stack_args (SLang_Num_Function_Args, 2, usage, &nargs))
     return;

   if ((-1 == SLang_pop_int (&n))
       || (-1 == SLang_pop_double (&bp.p)))
     return;

   if ((n < 0) || (bp.p < 0.0) || (bp.p > 1.0))
     {
	SLang_verror (SL_InvalidParm_Error, "rand_binomial assumes 0<=p<=1 and n>=0");
	return;
     }
   bp.n = (unsigned int)n;

   if (-1 == do_xxxrand (nargs, SLANG_UINT_TYPE,
			 generate_binomial_randoms, (VOID_STAR) &bp,
			 &is_scalar, &u))
     return;

   if (is_scalar)
     (void) SLang_push_uint (u);
}

/*}}}*/

static void srand_intrin (void) /*{{{*/
{
   SLang_MMT_Type *mmt = NULL;
   Rand_Type *r = Default_Rand;
   unsigned long seeds[NUM_SEEDS];
   int nargs = SLang_Num_Function_Args;

   if (-1 == pop_seeds (seeds))
     return;

   if (nargs == 2)
     {
	if (NULL == (mmt = SLang_pop_mmt (Rand_Type_Id)))
	  return;
	r = (Rand_Type *)SLang_object_from_mmt (mmt);
     }

   if (r != NULL)
     seed_random (r, seeds);

   if (mmt != NULL)
     SLang_free_mmt (mmt);
}

/*}}}*/

static void rand_permutation_intrin (void)
{
   int nargs = SLang_Num_Function_Args;
   Rand_Type *rt = Default_Rand;
   SLang_MMT_Type *mmt = NULL;
   SLang_Array_Type *at = NULL;
   int *data;
   SLindex_Type i, n;

   switch (nargs)
     {
      default:
	SLang_verror (SL_Usage_Error, "Usage: p = rand_permutation([Rand_Type,], n)");
	return;

      case 2:
      case 1:
	if (-1 == SLang_pop_array_index (&n))
	  return;
	if (nargs == 2)
	  {
	     if (NULL == (mmt = SLang_pop_mmt (Rand_Type_Id)))
	       return;

	     if (NULL == (rt = (Rand_Type *) SLang_object_from_mmt (mmt)))
	       goto free_return;
	  }
     }

   if (n < 0)
     {
	SLang_verror (SL_InvalidParm_Error, "rand_permutation: expected n>=0");
	goto free_return;
     }

   if (NULL == (at = SLang_create_array (SLANG_INT_TYPE, 0, NULL, &n, 1)))
     goto free_return;

   data = (int *) at->data;
   for (i = 0; i < n; i++)
     data[i] = i;

   /* Fisher-Yates */
   while (n > 1)
     {
	int k, p;

	k = (int) (n*uniform_random (rt));     /* 0 <= k < n */
	n--;
	p = data[n];
	data[n] = data[k];
	data[k] = p;
     }

   (void) SLang_push_array (at, 0);

free_return:
   if (at != NULL)
     SLang_free_array (at);
   if (mmt != NULL)
     SLang_free_mmt (mmt);
}

static SLang_Intrin_Fun_Type Module_Intrinsics [] =
{
   MAKE_INTRINSIC_0("rand", rand_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("srand", srand_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_uniform", urand_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_uniform_pos", urand_pos_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_gauss", rand_gauss_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_poisson", rand_poisson_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_gamma", rand_gamma_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_binomial", rand_binomial_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_beta", rand_beta_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_cauchy", rand_cauchy_intrin, SLANG_VOID_TYPE),
   MAKE_INTRINSIC_0("rand_geometric", rand_geometric_intrin, SLANG_VOID_TYPE),

   MAKE_INTRINSIC_0("rand_permutation", rand_permutation_intrin, SLANG_VOID_TYPE),

   MAKE_INTRINSIC_0("rand_new", new_rand_intrin, SLANG_VOID_TYPE),
   SLANG_END_INTRIN_FUN_TABLE
};

int init_rand_module_ns (char *ns_name)
{
   SLang_Class_Type *cl;

   SLang_NameSpace_Type *ns = SLns_create_namespace (ns_name);
   if (ns == NULL)
     return -1;

   if (Default_Rand == NULL)
     {
	unsigned long seeds[NUM_SEEDS];
	generate_seeds (seeds);
	Default_Rand = create_random (seeds);
	if (Default_Rand == NULL)
	  return -1;

	init_poisson ();
     }

   if (Rand_Type_Id == -1)
     {
	if (NULL == (cl = SLclass_allocate_class ("Rand_Type")))
	  return -1;

	(void) SLclass_set_destroy_function (cl, destroy_rand_type);

	if (-1 == SLclass_register_class (cl, SLANG_VOID_TYPE,
					  sizeof (Rand_Type),
					  SLANG_CLASS_TYPE_MMT))
	  return -1;

	Rand_Type_Id = SLclass_get_class_id (cl);
     }

   if (-1 == SLns_add_intrin_fun_table (ns, Module_Intrinsics, NULL))
     return -1;

   return 0;
}

/* This function is optional */
void deinit_rand_module (void)
{
}