Skip to content

Commit 845d078

Browse files
committed
added c program to generate lists of random chunks from GSL random number generators, these can serve as comparison to test correctness of implementation
1 parent 6d98fae commit 845d078

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

tests/gsl-rng-dump.c

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <gsl/gsl_rng.h>
4+
5+
/* Return the gsl_rng_type for the GSL random number generator with
6+
the provided name. */
7+
const gsl_rng_type* type_for_name (const char* name)
8+
{
9+
const gsl_rng_type **t, **t0;
10+
t0 = gsl_rng_types_setup ();
11+
for (t = t0; *t != 0; t++){
12+
if (strcmp((*t)->name, name) == 0) {
13+
return *t;
14+
}
15+
}
16+
return 0;
17+
}
18+
19+
/* Given the name of a GSL random number generator and (optionally) a
20+
seed, print the name, the seed, and the first n random chunks to
21+
stdout. */
22+
int main (int argc, char** argv)
23+
{
24+
const char * name;
25+
unsigned long seed;
26+
const gsl_rng_type * T;
27+
gsl_rng * r;
28+
29+
int i, n = 10;
30+
31+
if (argc < 2) {
32+
fprintf (stderr,
33+
"Expects one or two parameters. The first, a string, is the name of a random\n"
34+
"number generator and is mandatory. The second, an unsigned long, is a seed\n"
35+
"and is optional, if it is not supplied, the default seed is used.\n");
36+
exit (1);
37+
} else if (argc < 3) {
38+
seed = 0; // In GSL, 0 is replaced by the rng's default seed.
39+
} else {
40+
seed = atol(argv[2]);
41+
}
42+
name = argv[1];
43+
44+
gsl_rng_env_setup();
45+
46+
T = type_for_name (name);
47+
48+
if (T == 0) {
49+
fprintf (stderr, "\"%s\" is not the name of a gsl_rng_type.\n", name);
50+
exit (2);
51+
}
52+
53+
r = gsl_rng_alloc (T);
54+
gsl_rng_set(r, seed);
55+
56+
/* Print name of rng, the seed used to initialized it, and its
57+
first n random chunks. Items are separated by a <space> and
58+
terminated with a <newline>. */
59+
printf("\"%s\" %lu", name, seed);
60+
for (i = 0; i < n; i++) {
61+
unsigned long u = gsl_rng_get (r);
62+
printf (" %lu", u);
63+
}
64+
printf("\n");
65+
66+
gsl_rng_free (r);
67+
return 0;
68+
}

tests/gsl-rng-dump.csv

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"randu", 0, 65539, 393225, 1769499, 7077969, 26542323, 95552217, 334432395, 1146624417, 1722371299, 14608041
2+
"minstd", 0, 16807, 282475249, 1622650073, 984943658, 1144108930, 470211272, 101027544, 1457850878, 1458777923, 2007237709
3+
"mt19937", 0, 4293858116, 699692587, 1213834231, 4068197670, 994957275, 2082945813, 4112332215, 3196767107, 2319469851, 3178073856
4+
"mrg", 0, 572361259, 521023500, 563045572, 393759085, 1080953451, 130004609, 893178225, 1206078822, 397595998, 1497657786
5+
"cmrg", 0, 240037626, 2059795007, 1807165044, 1987289342, 591431996, 1523218864, 1189992209, 1891279779, 1282347090, 1614637363
6+
"transputer", 0, 1664525, 389569705, 2940799637, 158984081, 2862450781, 3211393721, 1851289957, 3934847009, 2184914861, 246739401
7+
"borosh13", 0, 1812433253, 88293849, 1790253981, 42330609, 3130934549, 318537289, 3711034317, 2922550497, 3385337285, 3700446137
8+
"fishman18", 0, 62089911, 847344462, 1061653656, 1954074819, 226824280, 953102500, 1452288378, 50913524, 2133871779, 1843965925
9+
"fishman20", 0, 48271, 182605794, 1291394886, 1914720637, 2078669041, 407355683, 1105902161, 854716505, 564586691, 1596680831
10+
"lecuyer21", 0, 40692, 1655838864, 2103410263, 1872071452, 652912057, 1780294415, 535353314, 525453832, 1422611300, 1336516156
11+
"waterman14", 0, 1566083941, 2203506137, 1324822941, 1986974193, 2643373845, 1922267721, 3584877005, 1128752353, 3108566981, 1963571129

tests/make-gsl-rng-dump.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gcc -Wall -I/usr/local/include -c gsl-rng-dump.c
2+
gcc -L/usr/local/lib gsl-rng-dump.o -lgsl -lm -o gsl-rng-dump

0 commit comments

Comments
 (0)