xref: /netbsd-src/external/lgpl3/gmp/dist/tests/rand/t-mt.c (revision 9573673d78c64ea1eac42d7f2e9521be89932ae5)
1 /* Test the Mersenne Twister random number generator.
2 
3 Copyright 2002 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 #include "tests.h"
24 
25 #ifndef TRUE
26 #define TRUE (1)
27 #endif
28 #ifndef FALSE
29 #define FALSE (0)
30 #endif
31 
32 /* Test that the sequence without seeding equals the sequence with the
33    default seed.  */
34 int
35 chk_default_seed (void)
36 {
37   gmp_randstate_t r1, r2;
38   mpz_t a, b;
39   int i;
40   int ok = TRUE;
41 
42   mpz_init2 (a, 19936L);
43   mpz_init2 (b, 19936L);
44 
45   gmp_randinit_mt (r1);
46   gmp_randinit_mt (r2);
47   gmp_randseed_ui (r2, 5489L); /* Must match DEFAULT_SEED in randmt.c */
48   for (i = 0; i < 3; i++)
49     {
50       /* Extract one whole buffer per iteration.  */
51       mpz_urandomb (a, r1, 19936L);
52       mpz_urandomb (b, r2, 19936L);
53       if (mpz_cmp (a, b) != 0)
54 	{
55 	  ok = FALSE;
56 	  printf ("Default seed fails in iteration %d\n", i);
57 	  break;
58 	}
59     }
60   gmp_randclear (r1);
61   gmp_randclear (r2);
62 
63   mpz_clear (a);
64   mpz_clear (b);
65   return ok;
66 }
67 
68 int
69 main (int argc, char *argv[])
70 {
71   int ok;
72 
73   tests_start ();
74 
75   ok = chk_default_seed ();
76 
77   tests_end ();
78 
79   if (ok)
80     return 0; /* pass */
81   else
82     return 1; /* fail */
83 }
84