Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / math / random.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2005 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include "random.h"
19 #include <time.h>
20 #include "xalloc.h"
21
22 static gsl_rng *rng;
23
24 void
25 random_init (void)
26 {
27 }
28
29 void
30 random_done (void)
31 {
32   if (rng != NULL)
33     gsl_rng_free (rng);
34 }
35
36 /* Returns the current random number generator. */
37 gsl_rng *
38 get_rng (void)
39 {
40   if (rng == NULL)
41     set_rng (time (0));
42   return rng;
43 }
44
45 /* Initializes or reinitializes the random number generator with
46    the given SEED. */
47 void
48 set_rng (unsigned long seed)
49 {
50   rng = gsl_rng_alloc (gsl_rng_mt19937);
51   if (rng == NULL)
52     xalloc_die ();
53   gsl_rng_set (rng, seed);
54 }