~ubuntu-branches/ubuntu/dapper/gsl-ref-html/dapper

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
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.54+ (gsl)
     from /home/bjg/gsl.redhat/doc/gsl-ref.texi on 14 September 2005 -->

<TITLE>GNU Scientific Library -- Reference Manual - Random Number Generation</TITLE>
<link href="gsl-ref_18.html" rel=Next>
<link href="gsl-ref_16.html" rel=Previous>
<link href="gsl-ref_toc.html" rel=ToC>

</HEAD>
<BODY>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_16.html">previous</A>, <A HREF="gsl-ref_18.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC268" HREF="gsl-ref_toc.html#TOC268">Random Number Generation</A></H1>
<P>
<A NAME="IDX1368"></A>

</P>
<P>
The library provides a large collection of random number generators
which can be accessed through a uniform interface.  Environment
variables allow you to select different generators and seeds at runtime,
so that you can easily switch between generators without needing to
recompile your program.  Each instance of a generator keeps track of its
own state, allowing the generators to be used in multi-threaded
programs.  Additional functions are available for transforming uniform
random numbers into samples from continuous or discrete probability
distributions such as the Gaussian, log-normal or Poisson distributions.

</P>
<P>
These functions are declared in the header file <TT>`gsl_rng.h'</TT>.

</P>



<H2><A NAME="SEC269" HREF="gsl-ref_toc.html#TOC269">General comments on random numbers</A></H2>

<P>
In 1988, Park and Miller wrote a paper entitled "Random number
generators: good ones are hard to find." [Commun. ACM, 31, 1192--1201].
Fortunately, some excellent random number generators are available,
though poor ones are still in common use.  You may be happy with the
system-supplied random number generator on your computer, but you should
be aware that as computers get faster, requirements on random number
generators increase.  Nowadays, a simulation that calls a random number
generator millions of times can often finish before you can make it down
the hall to the coffee machine and back.

</P>
<P>
A very nice review of random number generators was written by Pierre
L'Ecuyer, as Chapter 4 of the book: Handbook on Simulation, Jerry Banks,
ed. (Wiley, 1997).  The chapter is available in postscript from
L'Ecuyer's ftp site (see references).  Knuth's volume on Seminumerical
Algorithms (originally published in 1968) devotes 170 pages to random
number generators, and has recently been updated in its 3rd edition
(1997).
It is brilliant, a classic.  If you don't own it, you should stop reading
right now, run to the nearest bookstore, and buy it.

</P>
<P>
A good random number generator will satisfy both theoretical and
statistical properties.  Theoretical properties are often hard to obtain
(they require real math!), but one prefers a random number generator
with a long period, low serial correlation, and a tendency <EM>not</EM> to
"fall mainly on the planes."  Statistical tests are performed with
numerical simulations.  Generally, a random number generator is used to
estimate some quantity for which the theory of probability provides an
exact answer.  Comparison to this exact answer provides a measure of
"randomness".

</P>


<H2><A NAME="SEC270" HREF="gsl-ref_toc.html#TOC270">The Random Number Generator Interface</A></H2>

<P>
It is important to remember that a random number generator is not a
"real" function like sine or cosine.  Unlike real functions, successive
calls to a random number generator yield different return values.  Of
course that is just what you want for a random number generator, but to
achieve this effect, the generator must keep track of some kind of
"state" variable.  Sometimes this state is just an integer (sometimes
just the value of the previously generated random number), but often it
is more complicated than that and may involve a whole array of numbers,
possibly with some indices thrown in.  To use the random number
generators, you do not need to know the details of what comprises the
state, and besides that varies from algorithm to algorithm.

</P>
<P>
The random number generator library uses two special structs,
<CODE>gsl_rng_type</CODE> which holds static information about each type of
generator and <CODE>gsl_rng</CODE> which describes an instance of a generator
created from a given <CODE>gsl_rng_type</CODE>.

</P>
<P>
The functions described in this section are declared in the header file
<TT>`gsl_rng.h'</TT>.

</P>


<H2><A NAME="SEC271" HREF="gsl-ref_toc.html#TOC271">Random number generator initialization</A></H2>

<P>
<DL>
<DT><U>Function:</U> gsl_rng * <B>gsl_rng_alloc</B> <I>(const gsl_rng_type * <VAR>T</VAR>)</I>
<DD><A NAME="IDX1369"></A>
This function returns a pointer to a newly-created
instance of a random number generator of type <VAR>T</VAR>.
For example, the following code creates an instance of the Tausworthe
generator,

</P>

<PRE>
gsl_rng * r = gsl_rng_alloc (gsl_rng_taus);
</PRE>

<P>
If there is insufficient memory to create the generator then the
function returns a null pointer and the error handler is invoked with an
error code of <CODE>GSL_ENOMEM</CODE>.

</P>
<P>
The generator is automatically initialized with the default seed,
<CODE>gsl_rng_default_seed</CODE>.  This is zero by default but can be changed
either directly or by using the environment variable <CODE>GSL_RNG_SEED</CODE>
(see section <A HREF="gsl-ref_17.html#SEC274">Random number environment variables</A>).

</P>
<P>
The details of the available generator types are
described later in this chapter.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> void <B>gsl_rng_set</B> <I>(const gsl_rng * <VAR>r</VAR>, unsigned long int <VAR>s</VAR>)</I>
<DD><A NAME="IDX1370"></A>
This function initializes (or `seeds') the random number generator.  If
the generator is seeded with the same value of <VAR>s</VAR> on two different
runs, the same stream of random numbers will be generated by successive
calls to the routines below.  If different values of <VAR>s</VAR> are
supplied, then the generated streams of random numbers should be
completely different.  If the seed <VAR>s</VAR> is zero then the standard seed
from the original implementation is used instead.  For example, the
original Fortran source code for the <CODE>ranlux</CODE> generator used a seed
of 314159265, and so choosing <VAR>s</VAR> equal to zero reproduces this when
using <CODE>gsl_rng_ranlux</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> void <B>gsl_rng_free</B> <I>(gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1371"></A>
This function frees all the memory associated with the generator
<VAR>r</VAR>.
</DL>

</P>


<H2><A NAME="SEC272" HREF="gsl-ref_toc.html#TOC272">Sampling from a random number generator</A></H2>

<P>
The following functions return uniformly distributed random numbers,
either as integers or double precision floating point numbers.  To obtain
non-uniform distributions see section <A HREF="gsl-ref_19.html#SEC292">Random Number Distributions</A>.

</P>
<P>
<DL>
<DT><U>Function:</U> unsigned long int <B>gsl_rng_get</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1372"></A>
This function returns a random integer from the generator <VAR>r</VAR>.  The
minimum and maximum values depend on the algorithm used, but all
integers in the range [<VAR>min</VAR>,<VAR>max</VAR>] are equally likely.  The
values of <VAR>min</VAR> and <VAR>max</VAR> can determined using the auxiliary
functions <CODE>gsl_rng_max (r)</CODE> and <CODE>gsl_rng_min (r)</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> double <B>gsl_rng_uniform</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1373"></A>
This function returns a double precision floating point number uniformly
distributed in the range [0,1).  The range includes 0.0 but excludes 1.0.
The value is typically obtained by dividing the result of
<CODE>gsl_rng_get(r)</CODE> by <CODE>gsl_rng_max(r) + 1.0</CODE> in double
precision.  Some generators compute this ratio internally so that they
can provide floating point numbers with more than 32 bits of randomness
(the maximum number of bits that can be portably represented in a single
<CODE>unsigned long int</CODE>).
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> double <B>gsl_rng_uniform_pos</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1374"></A>
This function returns a positive double precision floating point number
uniformly distributed in the range (0,1), excluding both 0.0 and 1.0.
The number is obtained by sampling the generator with the algorithm of
<CODE>gsl_rng_uniform</CODE> until a non-zero value is obtained.  You can use
this function if you need to avoid a singularity at 0.0.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> unsigned long int <B>gsl_rng_uniform_int</B> <I>(const gsl_rng * <VAR>r</VAR>, unsigned long int <VAR>n</VAR>)</I>
<DD><A NAME="IDX1375"></A>
This function returns a random integer from 0 to <VAR>n</VAR>-1 inclusive.
All integers in the range [0,<VAR>n</VAR>-1] are equally likely, regardless
of the generator used.  An offset correction is applied so that zero is
always returned with the correct probability, for any minimum value of
the underlying generator.

</P>
<P>
If <VAR>n</VAR> is larger than the range of the generator then the function
calls the error handler with an error code of <CODE>GSL_EINVAL</CODE> and
returns zero.
</DL>

</P>


<H2><A NAME="SEC273" HREF="gsl-ref_toc.html#TOC273">Auxiliary random number generator functions</A></H2>
<P>
The following functions provide information about an existing
generator.  You should use them in preference to hard-coding the generator
parameters into your own code.

</P>
<P>
<DL>
<DT><U>Function:</U> const char * <B>gsl_rng_name</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1376"></A>
This function returns a pointer to the name of the generator.
For example,

</P>

<PRE>
printf ("r is a '%s' generator\n", 
        gsl_rng_name (r));
</PRE>

<P>
would print something like <CODE>r is a 'taus' generator</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> unsigned long int <B>gsl_rng_max</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1377"></A>
<CODE>gsl_rng_max</CODE> returns the largest value that <CODE>gsl_rng_get</CODE>
can return.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> unsigned long int <B>gsl_rng_min</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1378"></A>
<CODE>gsl_rng_min</CODE> returns the smallest value that <CODE>gsl_rng_get</CODE>
can return.  Usually this value is zero.  There are some generators with
algorithms that cannot return zero, and for these generators the minimum
value is 1.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> void * <B>gsl_rng_state</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1379"></A>
<DT><U>Function:</U> size_t <B>gsl_rng_size</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1380"></A>
These functions return a pointer to the state of generator <VAR>r</VAR> and
its size.  You can use this information to access the state directly.  For
example, the following code will write the state of a generator to a
stream,

</P>

<PRE>
void * state = gsl_rng_state (r);
size_t n = gsl_rng_size (r);
fwrite (state, n, 1, stream);
</PRE>

</DL>

<P>
<DL>
<DT><U>Function:</U> const gsl_rng_type ** <B>gsl_rng_types_setup</B> <I>(void)</I>
<DD><A NAME="IDX1381"></A>
This function returns a pointer to an array of all the available
generator types, terminated by a null pointer. The function should be
called once at the start of the program, if needed.  The following code
fragment shows how to iterate over the array of generator types to print
the names of the available algorithms,

</P>

<PRE>
const gsl_rng_type **t, **t0;

t0 = gsl_rng_types_setup ();

printf ("Available generators:\n");

for (t = t0; *t != 0; t++)
  {
    printf ("%s\n", (*t)-&#62;name);
  }
</PRE>

</DL>



<H2><A NAME="SEC274" HREF="gsl-ref_toc.html#TOC274">Random number environment variables</A></H2>

<P>
The library allows you to choose a default generator and seed from the
environment variables <CODE>GSL_RNG_TYPE</CODE> and <CODE>GSL_RNG_SEED</CODE> and
the function <CODE>gsl_rng_env_setup</CODE>.  This makes it easy try out
different generators and seeds without having to recompile your program.

</P>
<P>
<DL>
<DT><U>Function:</U> const gsl_rng_type * <B>gsl_rng_env_setup</B> <I>(void)</I>
<DD><A NAME="IDX1382"></A>
This function reads the environment variables <CODE>GSL_RNG_TYPE</CODE> and
<CODE>GSL_RNG_SEED</CODE> and uses their values to set the corresponding
library variables <CODE>gsl_rng_default</CODE> and
<CODE>gsl_rng_default_seed</CODE>.  These global variables are defined as
follows,

</P>

<PRE>
extern const gsl_rng_type *gsl_rng_default
extern unsigned long int gsl_rng_default_seed
</PRE>

<P>
The environment variable <CODE>GSL_RNG_TYPE</CODE> should be the name of a
generator, such as <CODE>taus</CODE> or <CODE>mt19937</CODE>.  The environment
variable <CODE>GSL_RNG_SEED</CODE> should contain the desired seed value.  It
is converted to an <CODE>unsigned long int</CODE> using the C library function
<CODE>strtoul</CODE>.

</P>
<P>
If you don't specify a generator for <CODE>GSL_RNG_TYPE</CODE> then
<CODE>gsl_rng_mt19937</CODE> is used as the default.  The initial value of
<CODE>gsl_rng_default_seed</CODE> is zero.

</P>
</DL>
<P>
Here is a short program which shows how to create a global
generator using the environment variables <CODE>GSL_RNG_TYPE</CODE> and
<CODE>GSL_RNG_SEED</CODE>,

</P>

<PRE>
#include &#60;stdio.h&#62;
#include &#60;gsl/gsl_rng.h&#62;

gsl_rng * r;  /* global generator */

int
main (void)
{
  const gsl_rng_type * T;

  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);
  
  printf ("generator type: %s\n", gsl_rng_name (r));
  printf ("seed = %lu\n", gsl_rng_default_seed);
  printf ("first value = %lu\n", gsl_rng_get (r));
  return 0;
}
</PRE>

<P>
Running the program without any environment variables uses the initial
defaults, an <CODE>mt19937</CODE> generator with a seed of 0,

</P>

<PRE>
$ ./a.out 
generator type: mt19937
seed = 0
first value = 4293858116
</PRE>

<P>
By setting the two variables on the command line we can
change the default generator and the seed,

</P>

<PRE>
$ GSL_RNG_TYPE="taus" GSL_RNG_SEED=123 ./a.out 
GSL_RNG_TYPE=taus
GSL_RNG_SEED=123
generator type: taus
seed = 123
first value = 2720986350
</PRE>



<H2><A NAME="SEC275" HREF="gsl-ref_toc.html#TOC275">Copying random number generator state</A></H2>

<P>
The above methods do not expose the random number `state' which changes
from call to call.  It is often useful to be able to save and restore
the state.  To permit these practices, a few somewhat more advanced
functions are supplied.  These include:

</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_rng_memcpy</B> <I>(gsl_rng * <VAR>dest</VAR>, const gsl_rng * <VAR>src</VAR>)</I>
<DD><A NAME="IDX1383"></A>
This function copies the random number generator <VAR>src</VAR> into the
pre-existing generator <VAR>dest</VAR>, making <VAR>dest</VAR> into an exact copy
of <VAR>src</VAR>.  The two generators must be of the same type.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> gsl_rng * <B>gsl_rng_clone</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1384"></A>
This function returns a pointer to a newly created generator which is an
exact copy of the generator <VAR>r</VAR>.
</DL>

</P>


<H2><A NAME="SEC276" HREF="gsl-ref_toc.html#TOC276">Reading and writing random number generator state</A></H2>

<P>
The library provides functions for reading and writing the random
number state to a file as binary data or formatted text.

</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_rng_fwrite</B> <I>(FILE * <VAR>stream</VAR>, const gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1385"></A>
This function writes the random number state of the random number
generator <VAR>r</VAR> to the stream <VAR>stream</VAR> in binary format.  The
return value is 0 for success and <CODE>GSL_EFAILED</CODE> if there was a
problem writing to the file.  Since the data is written in the native
binary format it may not be portable between different architectures.
</DL>

</P>
<P>
<DL>
<DT><U>Function:</U> int <B>gsl_rng_fread</B> <I>(FILE * <VAR>stream</VAR>, gsl_rng * <VAR>r</VAR>)</I>
<DD><A NAME="IDX1386"></A>
This function reads the random number state into the random number
generator <VAR>r</VAR> from the open stream <VAR>stream</VAR> in binary format.
The random number generator <VAR>r</VAR> must be preinitialized with the
correct random number generator type since type information is not
saved.  The return value is 0 for success and <CODE>GSL_EFAILED</CODE> if
there was a problem reading from the file.  The data is assumed to
have been written in the native binary format on the same
architecture.
</DL>

</P>


<H2><A NAME="SEC277" HREF="gsl-ref_toc.html#TOC277">Random number generator algorithms</A></H2>

<P>
The functions described above make no reference to the actual algorithm
used.  This is deliberate so that you can switch algorithms without
having to change any of your application source code.  The library
provides a large number of generators of different types, including
simulation quality generators, generators provided for compatibility
with other libraries and historical generators from the past.

</P>
<P>
The following generators are recommended for use in simulation.  They
have extremely long periods, low correlation and pass most statistical
tests.

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_mt19937</B>
<DD><A NAME="IDX1387"></A>
<A NAME="IDX1388"></A>
The MT19937 generator of Makoto Matsumoto and Takuji Nishimura is a
variant of the twisted generalized feedback shift-register algorithm,
and is known as the "Mersenne Twister" generator.  It has a Mersenne
prime period of 
2^19937 - 1 (about 
10^6000) and is
equi-distributed in 623 dimensions.  It has passed the DIEHARD
statistical tests.  It uses 624 words of state per generator and is
comparable in speed to the other generators.  The original generator used
a default seed of 4357 and choosing <VAR>s</VAR> equal to zero in
<CODE>gsl_rng_set</CODE> reproduces this.

</P>
<P>
For more information see,

<UL>
<LI>

Makoto Matsumoto and Takuji Nishimura, "Mersenne Twister: A
623-dimensionally equidistributed uniform pseudorandom number
generator". <CITE>ACM Transactions on Modeling and Computer
Simulation</CITE>, Vol. 8, No. 1 (Jan. 1998), Pages 3--30
</UL>

<P>
The generator <CODE>gsl_rng_mt19937</CODE> uses the second revision of the
seeding procedure published by the two authors above in 2002.  The
original seeding procedures could cause spurious artifacts for some seed
values. They are still available through the alternative generators
<CODE>gsl_rng_mt19937_1999</CODE> and <CODE>gsl_rng_mt19937_1998</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_ranlxs0</B>
<DD><A NAME="IDX1389"></A>
<DT><U>Generator:</U> <B>gsl_rng_ranlxs1</B>
<DD><A NAME="IDX1390"></A>
<DT><U>Generator:</U> <B>gsl_rng_ranlxs2</B>
<DD><A NAME="IDX1391"></A>
<A NAME="IDX1392"></A>

</P>
<P>
The generator <CODE>ranlxs0</CODE> is a second-generation version of the
RANLUX algorithm of L@"uscher, which produces "luxury random
numbers".  This generator provides single precision output (24 bits) at
three luxury levels <CODE>ranlxs0</CODE>, <CODE>ranlxs1</CODE> and <CODE>ranlxs2</CODE>.
It uses double-precision floating point arithmetic internally and can be
significantly faster than the integer version of <CODE>ranlux</CODE>,
particularly on 64-bit architectures.  The period of the generator is
about 
10^171.  The algorithm has mathematically proven properties and
can provide truly decorrelated numbers at a known level of randomness.
The higher luxury levels provide increased decorrelation between samples
as an additional safety margin.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_ranlxd1</B>
<DD><A NAME="IDX1393"></A>
<DT><U>Generator:</U> <B>gsl_rng_ranlxd2</B>
<DD><A NAME="IDX1394"></A>
<A NAME="IDX1395"></A>

</P>
<P>
These generators produce double precision output (48 bits) from the
RANLXS generator.  The library provides two luxury levels
<CODE>ranlxd1</CODE> and <CODE>ranlxd2</CODE>.
</DL>

</P>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_ranlux</B>
<DD><A NAME="IDX1396"></A>
<DT><U>Generator:</U> <B>gsl_rng_ranlux389</B>
<DD><A NAME="IDX1397"></A>

</P>
<P>
<A NAME="IDX1398"></A>
The <CODE>ranlux</CODE> generator is an implementation of the original
algorithm developed by L@"uscher.  It uses a
lagged-fibonacci-with-skipping algorithm to produce "luxury random
numbers".  It is a 24-bit generator, originally designed for
single-precision IEEE floating point numbers.  This implementation is
based on integer arithmetic, while the second-generation versions
RANLXS and RANLXD described above provide floating-point
implementations which will be faster on many platforms.
The period of the generator is about 
10^171.  The algorithm has mathematically proven properties and
it can provide truly decorrelated numbers at a known level of
randomness.  The default level of decorrelation recommended by L@"uscher
is provided by <CODE>gsl_rng_ranlux</CODE>, while <CODE>gsl_rng_ranlux389</CODE>
gives the highest level of randomness, with all 24 bits decorrelated.
Both types of generator use 24 words of state per generator.

</P>
<P>
For more information see,

<UL>
<LI>

M. L@"uscher, "A portable high-quality random number generator for
lattice field theory calculations", <CITE>Computer Physics
Communications</CITE>, 79 (1994) 100--110.
<LI>

F. James, "RANLUX: A Fortran implementation of the high-quality
pseudo-random number generator of L@"uscher", <CITE>Computer Physics
Communications</CITE>, 79 (1994) 111--114
</UL>

</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_cmrg</B>
<DD><A NAME="IDX1399"></A>
<A NAME="IDX1400"></A>
This is a combined multiple recursive generator by L'Ecuyer. 
Its sequence is,

</P>

<PRE>
z_n = (x_n - y_n) mod m_1
</PRE>

<P>
where the two underlying generators x_n and y_n are,

</P>

<PRE>
x_n = (a_1 x_{n-1} + a_2 x_{n-2} + a_3 x_{n-3}) mod m_1
y_n = (b_1 y_{n-1} + b_2 y_{n-2} + b_3 y_{n-3}) mod m_2
</PRE>

<P>
with coefficients 
a_1 = 0, 
a_2 = 63308, 
a_3 = -183326,
b_1 = 86098, 
b_2 = 0,
b_3 = -539608,
and moduli 
m_1 = 2^31 - 1 = 2147483647
and 
m_2 = 2145483479.

</P>
<P>
The period of this generator is 
2^205 
(about 
10^61).  It uses
6 words of state per generator.  For more information see,

</P>

<UL>
<LI>

P. L'Ecuyer, "Combined Multiple Recursive Random Number
Generators", <CITE>Operations Research</CITE>, 44, 5 (1996), 816--822.
</UL>

</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_mrg</B>
<DD><A NAME="IDX1401"></A>
<A NAME="IDX1402"></A>
This is a fifth-order multiple recursive generator by L'Ecuyer, Blouin
and Coutre.  Its sequence is,

</P>

<PRE>
x_n = (a_1 x_{n-1} + a_5 x_{n-5}) mod m
</PRE>

<P>
with 
a_1 = 107374182, 
a_2 = a_3 = a_4 = 0, 
a_5 = 104480
and 
m = 2^31 - 1.

</P>
<P>
The period of this generator is about 
10^46.  It uses 5 words
of state per generator.  More information can be found in the following
paper,

<UL>
<LI>

P. L'Ecuyer, F. Blouin, and R. Coutre, "A search for good multiple
recursive random number generators", <CITE>ACM Transactions on Modeling and
Computer Simulation</CITE> 3, 87--98 (1993).
</UL>

</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_taus</B>
<DD><A NAME="IDX1403"></A>
<DT><U>Generator:</U> <B>gsl_rng_taus2</B>
<DD><A NAME="IDX1404"></A>
<A NAME="IDX1405"></A>
This is a maximally equidistributed combined Tausworthe generator by
L'Ecuyer.  The sequence is,

</P>

<PRE>
x_n = (s1_n ^^ s2_n ^^ s3_n) 
</PRE>

<P>
where,

</P>

<PRE>
s1_{n+1} = (((s1_n&#38;4294967294)&#60;&#60;12)^^(((s1_n&#60;&#60;13)^^s1_n)&#62;&#62;19))
s2_{n+1} = (((s2_n&#38;4294967288)&#60;&#60; 4)^^(((s2_n&#60;&#60; 2)^^s2_n)&#62;&#62;25))
s3_{n+1} = (((s3_n&#38;4294967280)&#60;&#60;17)^^(((s3_n&#60;&#60; 3)^^s3_n)&#62;&#62;11))
</PRE>

<P>
computed modulo 
2^32.  In the formulas above 
^^
denotes "exclusive-or".  Note that the algorithm relies on the properties
of 32-bit unsigned integers and has been implemented using a bitmask
of <CODE>0xFFFFFFFF</CODE> to make it work on 64 bit machines.

</P>
<P>
The period of this generator is 
2^88 (about
10^26).  It uses 3 words of state per generator.  For more
information see,

</P>

<UL>
<LI>

P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
Generators", <CITE>Mathematics of Computation</CITE>, 65, 213 (1996), 203--213.
</UL>

<P>
The generator <CODE>gsl_rng_taus2</CODE> uses the same algorithm as
<CODE>gsl_rng_taus</CODE> but with an improved seeding procedure described in
the paper,

</P>

<UL>
<LI>

P. L'Ecuyer, "Tables of Maximally Equidistributed Combined LFSR
Generators", <CITE>Mathematics of Computation</CITE>, 68, 225 (1999), 261--269
</UL>

<P>
The generator <CODE>gsl_rng_taus2</CODE> should now be used in preference to
<CODE>gsl_rng_taus</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_gfsr4</B>
<DD><A NAME="IDX1406"></A>
<A NAME="IDX1407"></A>
The <CODE>gfsr4</CODE> generator is like a lagged-fibonacci generator, and 
produces each number as an <CODE>xor</CODE>'d sum of four previous values.

</P>

<PRE>
r_n = r_{n-A} ^^ r_{n-B} ^^ r_{n-C} ^^ r_{n-D}
</PRE>

<P>
Ziff (ref below) notes that "it is now widely known" that two-tap
registers (such as R250, which is described below)
have serious flaws, the most obvious one being the three-point
correlation that comes from the definition of the generator.  Nice
mathematical properties can be derived for GFSR's, and numerics bears
out the claim that 4-tap GFSR's with appropriately chosen offsets are as
random as can be measured, using the author's test.

</P>
<P>
This implementation uses the values suggested the example on p392 of
Ziff's article: A=471, B=1586, C=6988, D=9689.

</P>

<P>
If the offsets are appropriately chosen (such as the one ones in this
implementation), then the sequence is said to be maximal; that means
that the period is 2^D - 1, where D is the longest lag.
(It is one less than 2^D because it is not permitted to have all
zeros in the <CODE>ra[]</CODE> array.)  For this implementation with
D=9689 that works out to about 
10^2917.

</P>
<P>
Note that the implementation of this generator using a 32-bit
integer amounts to 32 parallel implementations of one-bit
generators.  One consequence of this is that the period of this
32-bit generator is the same as for the one-bit generator.
Moreover, this independence means that all 32-bit patterns are
equally likely, and in particular that 0 is an allowed random
value.  (We are grateful to Heiko Bauke for clarifying for us these
properties of GFSR random number generators.)

</P>
<P>
For more information see,

<UL>
<LI>

Robert M. Ziff, "Four-tap shift-register-sequence random-number 
generators", <CITE>Computers in Physics</CITE>, 12(4), Jul/Aug
1998, pp 385--392.
</UL>

</DL>



<H2><A NAME="SEC278" HREF="gsl-ref_toc.html#TOC278">Unix random number generators</A></H2>

<P>
The standard Unix random number generators <CODE>rand</CODE>, <CODE>random</CODE>
and <CODE>rand48</CODE> are provided as part of GSL. Although these
generators are widely available individually often they aren't all
available on the same platform.  This makes it difficult to write
portable code using them and so we have included the complete set of
Unix generators in GSL for convenience.  Note that these generators
don't produce high-quality randomness and aren't suitable for work
requiring accurate statistics.  However, if you won't be measuring
statistical quantities and just want to introduce some variation into
your program then these generators are quite acceptable.

</P>
<P>
<A NAME="IDX1408"></A>
<A NAME="IDX1409"></A>
<A NAME="IDX1410"></A>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_rand</B>
<DD><A NAME="IDX1411"></A>
<A NAME="IDX1412"></A>
This is the BSD <CODE>rand()</CODE> generator.  Its sequence is

</P>

<PRE>
x_{n+1} = (a x_n + c) mod m
</PRE>

<P>
with 
a = 1103515245, 
c = 12345 and 
m = 2^31.
The seed specifies the initial value, 
x_1.  The period of this
generator is 
2^31, and it uses 1 word of storage per
generator.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_random_bsd</B>
<DD><A NAME="IDX1413"></A>
<DT><U>Generator:</U> <B>gsl_rng_random_libc5</B>
<DD><A NAME="IDX1414"></A>
<DT><U>Generator:</U> <B>gsl_rng_random_glibc2</B>
<DD><A NAME="IDX1415"></A>
These generators implement the <CODE>random()</CODE> family of functions, a
set of linear feedback shift register generators originally used in BSD
Unix.  There are several versions of <CODE>random()</CODE> in use today: the
original BSD version (e.g. on SunOS4), a libc5 version (found on
older GNU/Linux systems) and a glibc2 version.  Each version uses a
different seeding procedure, and thus produces different sequences.

</P>
<P>
The original BSD routines accepted a variable length buffer for the
generator state, with longer buffers providing higher-quality
randomness.  The <CODE>random()</CODE> function implemented algorithms for
buffer lengths of 8, 32, 64, 128 and 256 bytes, and the algorithm with
the largest length that would fit into the user-supplied buffer was
used.  To support these algorithms additional generators are available
with the following names,

</P>

<PRE>
gsl_rng_random8_bsd
gsl_rng_random32_bsd
gsl_rng_random64_bsd
gsl_rng_random128_bsd
gsl_rng_random256_bsd
</PRE>

<P>
where the numeric suffix indicates the buffer length.  The original BSD
<CODE>random</CODE> function used a 128-byte default buffer and so
<CODE>gsl_rng_random_bsd</CODE> has been made equivalent to
<CODE>gsl_rng_random128_bsd</CODE>.  Corresponding versions of the <CODE>libc5</CODE>
and <CODE>glibc2</CODE> generators are also available, with the names
<CODE>gsl_rng_random8_libc5</CODE>, <CODE>gsl_rng_random8_glibc2</CODE>, etc.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_rand48</B>
<DD><A NAME="IDX1416"></A>
<A NAME="IDX1417"></A>
This is the Unix <CODE>rand48</CODE> generator.  Its sequence is

</P>

<PRE>
x_{n+1} = (a x_n + c) mod m
</PRE>

<P>
defined on 48-bit unsigned integers with 
a = 25214903917, 
c = 11 and 
m = 2^48. 
The seed specifies the upper 32 bits of the initial value, x_1,
with the lower 16 bits set to <CODE>0x330E</CODE>.  The function
<CODE>gsl_rng_get</CODE> returns the upper 32 bits from each term of the
sequence.  This does not have a direct parallel in the original
<CODE>rand48</CODE> functions, but forcing the result to type <CODE>long int</CODE>
reproduces the output of <CODE>mrand48</CODE>.  The function
<CODE>gsl_rng_uniform</CODE> uses the full 48 bits of internal state to return
the double precision number x_n/m, which is equivalent to the
function <CODE>drand48</CODE>.  Note that some versions of the GNU C Library
contained a bug in <CODE>mrand48</CODE> function which caused it to produce
different results (only the lower 16-bits of the return value were set).
</DL>

</P>


<H2><A NAME="SEC279" HREF="gsl-ref_toc.html#TOC279">Other random number generators</A></H2>

<P>
The generators in this section are provided for compatibility with
existing libraries.  If you are converting an existing program to use GSL
then you can select these generators to check your new implementation
against the original one, using the same random number generator.  After
verifying that your new program reproduces the original results you can
then switch to a higher-quality generator.

</P>
<P>
Note that most of the generators in this section are based on single
linear congruence relations, which are the least sophisticated type of
generator.  In particular, linear congruences have poor properties when
used with a non-prime modulus, as several of these routines do (e.g.
with a power of two modulus, 
2^31 or 
2^32).  This
leads to periodicity in the least significant bits of each number,
with only the higher bits having any randomness.  Thus if you want to
produce a random bitstream it is best to avoid using the least
significant bits.

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_ranf</B>
<DD><A NAME="IDX1418"></A>
<A NAME="IDX1419"></A>
<A NAME="IDX1420"></A>
This is the CRAY random number generator <CODE>RANF</CODE>.  Its sequence is

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
defined on 48-bit unsigned integers with a = 44485709377909 and
m = 2^48.  The seed specifies the lower
32 bits of the initial value, 
x_1, with the lowest bit set to
prevent the seed taking an even value.  The upper 16 bits of 
x_1
are set to 0. A consequence of this procedure is that the pairs of seeds
2 and 3, 4 and 5, etc produce the same sequences.

</P>
<P>
The generator compatible with the CRAY MATHLIB routine RANF. It
produces double precision floating point numbers which should be
identical to those from the original RANF.

</P>
<P>
There is a subtlety in the implementation of the seeding.  The initial
state is reversed through one step, by multiplying by the modular
inverse of a mod m.  This is done for compatibility with
the original CRAY implementation.

</P>
<P>
Note that you can only seed the generator with integers up to
2^32, while the original CRAY implementation uses
non-portable wide integers which can cover all 
2^48 states of the generator.

</P>
<P>
The function <CODE>gsl_rng_get</CODE> returns the upper 32 bits from each term
of the sequence.  The function <CODE>gsl_rng_uniform</CODE> uses the full 48
bits to return the double precision number x_n/m.

</P>
<P>
The period of this generator is 
2^46.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_ranmar</B>
<DD><A NAME="IDX1421"></A>
<A NAME="IDX1422"></A>
This is the RANMAR lagged-fibonacci generator of Marsaglia, Zaman and
Tsang.  It is a 24-bit generator, originally designed for
single-precision IEEE floating point numbers.  It was included in the
CERNLIB high-energy physics library.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_r250</B>
<DD><A NAME="IDX1423"></A>
<A NAME="IDX1424"></A>
<A NAME="IDX1425"></A>
This is the shift-register generator of Kirkpatrick and Stoll.  The
sequence is based on the recurrence

</P>

<PRE>
x_n = x_{n-103} ^^ x_{n-250}
</PRE>

<P>
where 
^^ denotes "exclusive-or", defined on
32-bit words.  The period of this generator is about 
2^250 and it
uses 250 words of state per generator.

</P>
<P>
For more information see,

<UL>
<LI>

S. Kirkpatrick and E. Stoll, "A very fast shift-register sequence random
number generator", <CITE>Journal of Computational Physics</CITE>, 40, 517--526
(1981)
</UL>

</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_tt800</B>
<DD><A NAME="IDX1426"></A>
<A NAME="IDX1427"></A>
This is an earlier version of the twisted generalized feedback
shift-register generator, and has been superseded by the development of
MT19937.  However, it is still an acceptable generator in its own
right.  It has a period of 
2^800 and uses 33 words of storage
per generator.

</P>
<P>
For more information see,

<UL>
<LI>

Makoto Matsumoto and Yoshiharu Kurita, "Twisted GFSR Generators
II", <CITE>ACM Transactions on Modelling and Computer Simulation</CITE>,
Vol. 4, No. 3, 1994, pages 254--266.
</UL>

</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_vax</B>
<DD><A NAME="IDX1428"></A>
<A NAME="IDX1429"></A>
This is the VAX generator <CODE>MTH$RANDOM</CODE>.  Its sequence is,

</P>

<PRE>
x_{n+1} = (a x_n + c) mod m
</PRE>

<P>
with 
a = 69069, c = 1 and 
m = 2^32.  The seed specifies the initial value, 
x_1.  The
period of this generator is 
2^32 and it uses 1 word of storage per
generator.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_transputer</B>
<DD><A NAME="IDX1430"></A>
This is the random number generator from the INMOS Transputer
Development system.  Its sequence is,

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 1664525 and 
m = 2^32.
The seed specifies the initial value, 
x_1.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_randu</B>
<DD><A NAME="IDX1431"></A>
<A NAME="IDX1432"></A>
This is the IBM <CODE>RANDU</CODE> generator.  Its sequence is

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 65539 and 
m = 2^31.  The
seed specifies the initial value, 
x_1.  The period of this
generator was only 
2^29.  It has become a textbook example of a
poor generator.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_minstd</B>
<DD><A NAME="IDX1433"></A>
<A NAME="IDX1434"></A>
This is Park and Miller's "minimal standard" MINSTD generator, a
simple linear congruence which takes care to avoid the major pitfalls of
such algorithms.  Its sequence is,

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 16807 and 
m = 2^31 - 1 = 2147483647. 
The seed specifies the initial value, 
x_1.  The period of this
generator is about 
2^31.

</P>
<P>
This generator is used in the IMSL Library (subroutine RNUN) and in
MATLAB (the RAND function).  It is also sometimes known by the acronym
"GGL" (I'm not sure what that stands for).

</P>
<P>
For more information see,

<UL>
<LI>

Park and Miller, "Random Number Generators: Good ones are hard to find",
<CITE>Communications of the ACM</CITE>, October 1988, Volume 31, No 10, pages
1192--1201.
</UL>

</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_uni</B>
<DD><A NAME="IDX1435"></A>
<DT><U>Generator:</U> <B>gsl_rng_uni32</B>
<DD><A NAME="IDX1436"></A>
This is a reimplementation of the 16-bit SLATEC random number generator
RUNIF. A generalization of the generator to 32 bits is provided by
<CODE>gsl_rng_uni32</CODE>.  The original source code is available from NETLIB.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_slatec</B>
<DD><A NAME="IDX1437"></A>
This is the SLATEC random number generator RAND. It is ancient.  The
original source code is available from NETLIB.
</DL>

</P>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_zuf</B>
<DD><A NAME="IDX1438"></A>
This is the ZUFALL lagged Fibonacci series generator of Peterson.  Its
sequence is,

</P>

<PRE>
t = u_{n-273} + u_{n-607}
u_n  = t - floor(t)
</PRE>

<P>
The original source code is available from NETLIB.  For more information
see,

<UL>
<LI>

W. Petersen, "Lagged Fibonacci Random Number Generators for the NEC
SX-3", <CITE>International Journal of High Speed Computing</CITE> (1994).
</UL>

</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_borosh13</B>
<DD><A NAME="IDX1439"></A>
This is the Borosh-Niederreiter random number generator. It is taken
from Knuth's <CITE>Seminumerical Algorithms</CITE>, 3rd Ed., pages
106--108. Its sequence is,

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 1812433253 and 
m = 2^32.
The seed specifies the initial value, 
x_1.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_coveyou</B>
<DD><A NAME="IDX1440"></A>
This is the Coveyou random number generator. It is taken from Knuth's
<CITE>Seminumerical Algorithms</CITE>, 3rd Ed., Section 3.2.2. Its sequence
is,

</P>

<PRE>
x_{n+1} = (x_n (x_n + 1)) mod m
</PRE>

<P>
with 
m = 2^32.
The seed specifies the initial value, 
x_1.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_fishman18</B>
<DD><A NAME="IDX1441"></A>
This is the Fishman, Moore III random number generator. It is taken from
Knuth's <CITE>Seminumerical Algorithms</CITE>, 3rd Ed., pages 106--108. Its
sequence is,

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 62089911 and 
m = 2^31 - 1.
The seed specifies the initial value, 
x_1.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_fishman20</B>
<DD><A NAME="IDX1442"></A>
This is the Fishman random number generator. It is taken from Knuth's
<CITE>Seminumerical Algorithms</CITE>, 3rd Ed., page 108. Its sequence is,

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 48271 and 
m = 2^31 - 1.
The seed specifies the initial value, 
x_1.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_fishman2x</B>
<DD><A NAME="IDX1443"></A>
This is the L'Ecuyer--Fishman random number generator. It is taken from
Knuth's <CITE>Seminumerical Algorithms</CITE>, 3rd Ed., page 108. Its sequence
is,

</P>

<PRE>
z_{n+1} = (x_n - y_n) mod m
</PRE>

<P>
with 
m = 2^31 - 1.
x_n and y_n are given by the <CODE>fishman20</CODE> 
and <CODE>lecuyer21</CODE> algorithms.
The seed specifies the initial value, 
x_1.

</P>
</DL>

<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_knuthran2</B>
<DD><A NAME="IDX1444"></A>
This is a second-order multiple recursive generator described by Knuth
in <CITE>Seminumerical Algorithms</CITE>, 3rd Ed., page 108.  Its sequence is,

</P>

<PRE>
x_n = (a_1 x_{n-1} + a_2 x_{n-2}) mod m
</PRE>

<P>
with 
a_1 = 271828183, 
a_2 = 314159269, 
and 
m = 2^31 - 1.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_knuthran</B>
<DD><A NAME="IDX1445"></A>
This is a second-order multiple recursive generator described by Knuth
in <CITE>Seminumerical Algorithms</CITE>, 3rd Ed., Section 3.6.  Knuth
provides its C code.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_lecuyer21</B>
<DD><A NAME="IDX1446"></A>
This is the L'Ecuyer random number generator. It is taken from Knuth's
<CITE>Seminumerical Algorithms</CITE>, 3rd Ed., page 106--108. Its sequence is,

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 40692 and 
m = 2^31 - 249.
The seed specifies the initial value, 
x_1.
</DL>

</P>
<P>
<DL>
<DT><U>Generator:</U> <B>gsl_rng_waterman14</B>
<DD><A NAME="IDX1447"></A>
This is the Waterman random number generator. It is taken from Knuth's
<CITE>Seminumerical Algorithms</CITE>, 3rd Ed., page 106--108. Its sequence is,

</P>

<PRE>
x_{n+1} = (a x_n) mod m
</PRE>

<P>
with a = 1566083941 and 
m = 2^32.
The seed specifies the initial value, 
x_1.
</DL>

</P>



<H2><A NAME="SEC280" HREF="gsl-ref_toc.html#TOC280">Performance</A></H2>

<P>
The following table shows the relative performance of a selection the
available random number generators.  The fastest simulation quality
generators are <CODE>taus</CODE>, <CODE>gfsr4</CODE> and <CODE>mt19937</CODE>.  The
generators which offer the best mathematically-proven quality are those
based on the RANLUX algorithm.

</P>


<PRE>
1754 k ints/sec,    870 k doubles/sec, taus
1613 k ints/sec,    855 k doubles/sec, gfsr4
1370 k ints/sec,    769 k doubles/sec, mt19937
 565 k ints/sec,    571 k doubles/sec, ranlxs0
 400 k ints/sec,    405 k doubles/sec, ranlxs1
 490 k ints/sec,    389 k doubles/sec, mrg
 407 k ints/sec,    297 k doubles/sec, ranlux
 243 k ints/sec,    254 k doubles/sec, ranlxd1
 251 k ints/sec,    253 k doubles/sec, ranlxs2
 238 k ints/sec,    215 k doubles/sec, cmrg
 247 k ints/sec,    198 k doubles/sec, ranlux389
 141 k ints/sec,    140 k doubles/sec, ranlxd2

1852 k ints/sec,    935 k doubles/sec, ran3
 813 k ints/sec,    575 k doubles/sec, ran0
 787 k ints/sec,    476 k doubles/sec, ran1
 379 k ints/sec,    292 k doubles/sec, ran2
</PRE>



<H2><A NAME="SEC281" HREF="gsl-ref_toc.html#TOC281">Examples</A></H2>

<P>
The following program demonstrates the use of a random number generator
to produce uniform random numbers in the range [0.0, 1.0),

</P>

<PRE>
#include &#60;stdio.h&#62;
#include &#60;gsl/gsl_rng.h&#62;

int
main (void)
{
  const gsl_rng_type * T;
  gsl_rng * r;

  int i, n = 10;

  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  for (i = 0; i &#60; n; i++) 
    {
      double u = gsl_rng_uniform (r);
      printf ("%.5f\n", u);
    }

  gsl_rng_free (r);

  return 0;
}
</PRE>

<P>
Here is the output of the program,

</P>

<PRE>
$ ./a.out 
0.99974
0.16291
0.28262
0.94720
0.23166
0.48497
0.95748
0.74431
0.54004
0.73995
</PRE>

<P>
The numbers depend on the seed used by the generator.  The default seed
can be changed with the <CODE>GSL_RNG_SEED</CODE> environment variable to
produce a different stream of numbers.  The generator itself can be
changed using the environment variable <CODE>GSL_RNG_TYPE</CODE>.  Here is the
output of the program using a seed value of 123 and the
multiple-recursive generator <CODE>mrg</CODE>,

</P>

<PRE>
$ GSL_RNG_SEED=123 GSL_RNG_TYPE=mrg ./a.out 
GSL_RNG_TYPE=mrg
GSL_RNG_SEED=123
0.33050
0.86631
0.32982
0.67620
0.53391
0.06457
0.16847
0.70229
0.04371
0.86374
</PRE>



<H2><A NAME="SEC282" HREF="gsl-ref_toc.html#TOC282">References and Further Reading</A></H2>

<P>
The subject of random number generation and testing is reviewed
extensively in Knuth's <CITE>Seminumerical Algorithms</CITE>.

</P>

<UL>
<LI>

Donald E. Knuth, <CITE>The Art of Computer Programming: Seminumerical
Algorithms</CITE> (Vol 2, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896842.
</UL>

<P>
Further information is available in the review paper written by Pierre
L'Ecuyer,

</P>

<UL>
P. L'Ecuyer, "Random Number Generation", Chapter 4 of the
Handbook on Simulation, Jerry Banks Ed., Wiley, 1998, 93--137.

<A HREF="http://www.iro.umontreal.ca/~lecuyer/papers.html"><TT>http://www.iro.umontreal.ca/~lecuyer/papers.html</TT></A>
in the file <TT>`handsim.ps'</TT>.
</UL>

<P>
The source code for the DIEHARD random number generator tests is also
available online,

</P>

<UL>
<LI>

<CITE>DIEHARD source code</CITE> G. Marsaglia,
<LI>

<A HREF="http://stat.fsu.edu/pub/diehard/"><TT>http://stat.fsu.edu/pub/diehard/</TT></A>
</UL>

<P>
A comprehensive set of random number generator tests is available from
NIST,

</P>

<UL>
<LI>

NIST Special Publication 800-22, "A Statistical Test Suite for the
Validation of Random Number Generators and Pseudo Random Number
Generators for Cryptographic Applications".
<LI>

<A HREF="http://csrc.nist.gov/rng/"><TT>http://csrc.nist.gov/rng/</TT></A>
</UL>



<H2><A NAME="SEC283" HREF="gsl-ref_toc.html#TOC283">Acknowledgements</A></H2>

<P>
Thanks to Makoto Matsumoto, Takuji Nishimura and Yoshiharu Kurita for
making the source code to their generators (MT19937, MM&#38;TN; TT800,
MM&#38;YK) available under the GNU General Public License.  Thanks to Martin
L@"uscher for providing notes and source code for the RANLXS and
RANLXD generators.

</P>

<P><HR><P>
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_16.html">previous</A>, <A HREF="gsl-ref_18.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
</BODY>
</HTML>