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