1321936Shselasky/* t-rand -- Test random number generators.  */
2321936Shselasky
3321936Shselasky/*
4321936ShselaskyCopyright 2000, 2001 Free Software Foundation, Inc.
5321936Shselasky
6321936ShselaskyThis file is part of the GNU MP Library.
7321936Shselasky
8321936ShselaskyThe GNU MP Library is free software; you can redistribute it and/or modify
9321936Shselaskyit under the terms of the GNU Lesser General Public License as published by
10321936Shselaskythe Free Software Foundation; either version 3 of the License, or (at your
11321936Shselaskyoption) any later version.
12321936Shselasky
13321936ShselaskyThe GNU MP Library is distributed in the hope that it will be useful, but
14321936ShselaskyWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15321936Shselaskyor FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16321936ShselaskyLicense for more details.
17321936Shselasky
18321936ShselaskyYou should have received a copy of the GNU Lesser General Public License
19321936Shselaskyalong with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20321936Shselasky
21321936Shselasky#include <stdlib.h>
22321936Shselasky#include <stdio.h>
23321936Shselasky#include "gmp.h"
24321936Shselasky
25321936Shselasky#define SEED 1
26321936Shselasky#define BASE 16
27321936Shselasky#define ENTS 10			/* Number of entries in array when
28321936Shselasky				   printing.  */
29321936Shselasky
30321936Shselasky/* These were generated by this very program.  Do not edit!  */
31321936Shselasky/* Integers.  */
32321936Shselaskychar *z1[ENTS] = {"0", "1", "1", "1", "1", "0", "1", "1", "1", "1"};
33321936Shselaskychar *z2[ENTS] = {"0", "3", "1", "3", "3", "0", "3", "3", "3", "1"};
34321936Shselaskychar *z3[ENTS] = {"4", "3", "1", "7", "3", "0", "3", "3", "3", "1"};
35321936Shselaskychar *z4[ENTS] = {"c", "3", "1", "f", "b", "8", "3", "3", "3", "1"};
36321936Shselaskychar *z5[ENTS] = {"1c", "13", "11", "1f", "b", "18", "3", "13", "3", "1"};
37321936Shselasky
38321936Shselaskychar *z10[ENTS] = {"29c", "213", "f1", "17f", "12b", "178", "383", "d3", "3a3", "281"};
39321936Shselasky
40321936Shselaskychar *z15[ENTS] = {"29c", "1a13", "74f1", "257f", "592b", "4978", "4783", "7cd3", "5ba3", "4681"};
41321936Shselaskychar *z16[ENTS] = {"29c", "9a13", "74f1", "a57f", "d92b", "4978", "c783", "fcd3", "5ba3", "c681"};
42321936Shselaskychar *z17[ENTS] = {"51e", "f17a", "54ff", "1a335", "cf65", "5d6f", "583f", "618f", "1bc6", "98ff"};
43321936Shselasky
44321936Shselaskychar *z31[ENTS] = {"3aecd515", "13ae8ec6", "518c8090", "81ca077", "70b7134", "7ee78d71", "323a7636", "2122cb1a", "19811941", "41fd605"};
45321936Shselaskychar *z32[ENTS] = {"baecd515", "13ae8ec6", "518c8090", "881ca077", "870b7134", "7ee78d71", "323a7636", "a122cb1a", "99811941", "841fd605"};
46321936Shselaskychar *z33[ENTS] = {"1faf4cca", "15d6ef83b", "9095fe72", "1b6a3dff6", "b17cbddd", "16e5209d4", "6f65b12c", "493bbbc6", "abf2a5d5", "6d491a3c"};
47321936Shselasky
48321936Shselaskychar *z63[ENTS] = {"48a74f367fa7b5c8", "3ba9e9dc1b263076", "1e0ac84e7678e0fb", "11416581728b3e35", "36ab610523f0f1f7", "3e540e8e95c0eb4b", "439ae16057dbc9d3", "734fb260db243950", "7d3a317effc289bf", "1d80301fb3d1a0d1"};
49321936Shselaskychar *z64[ENTS] = {"48a74f367fa7b5c8", "bba9e9dc1b263076", "9e0ac84e7678e0fb", "11416581728b3e35", "b6ab610523f0f1f7", "be540e8e95c0eb4b", "439ae16057dbc9d3", "f34fb260db243950", "fd3a317effc289bf", "1d80301fb3d1a0d1"};
50321936Shselaskychar *z65[ENTS] = {"1ff77710d846d49f0", "1b1411701d709ee10", "31ffa81a208b6af4", "446638d431d3c681", "df5c569d5baa8b55", "197d99ea9bf28e5a0", "191ade09edd94cfae", "194acefa6dde5e18d", "1afc1167c56272d92", "d092994da72f206f"};
51321936Shselasky
52321936Shselaskychar *z127[ENTS] = {"2f66ba932aaf58a071fd8f0742a99a0c", "73cfa3c664c9c1753507ca60ec6b8425", "53ea074ca131dec12cd68b8aa8e20278", "3cf5ac8c343532f8a53cc0eb47581f73", "50c11d5869e208aa1b9aa317b8c2d0a9", "b23163c892876472b1ef19642eace09", "489f4c03d41f87509c8d6c90ce674f95", "2ab8748c96aa6762ea1932b44c9d7164", "98cb5591fc05ad31afbbc1d67b90edd", "77848bb991fd0be331adcf1457fbc672"};
53321936Shselaskychar *z128[ENTS] = {"af66ba932aaf58a071fd8f0742a99a0c", "73cfa3c664c9c1753507ca60ec6b8425", "53ea074ca131dec12cd68b8aa8e20278", "3cf5ac8c343532f8a53cc0eb47581f73", "50c11d5869e208aa1b9aa317b8c2d0a9", "8b23163c892876472b1ef19642eace09", "489f4c03d41f87509c8d6c90ce674f95", "aab8748c96aa6762ea1932b44c9d7164", "98cb5591fc05ad31afbbc1d67b90edd", "f7848bb991fd0be331adcf1457fbc672"};
54321936Shselasky
55321936Shselasky/* Floats.  */
56321936Shselaskychar *f1[ENTS] = {"0.@0", "0.8@0", "0.8@0", "0.8@0", "0.8@0", "0.@0", "0.8@0", "0.8@0", "0.8@0", "0.8@0"};
57321936Shselaskychar *f2[ENTS] = {"0.@0", "0.c@0", "0.4@0", "0.c@0", "0.c@0", "0.@0", "0.c@0", "0.c@0", "0.c@0", "0.4@0"};
58321936Shselaskychar *f3[ENTS] = {"0.8@0", "0.6@0", "0.2@0", "0.e@0", "0.6@0", "0.@0", "0.6@0", "0.6@0", "0.6@0", "0.2@0"};
59321936Shselaskychar *f4[ENTS] = {"0.c@0", "0.3@0", "0.1@0", "0.f@0", "0.b@0", "0.8@0", "0.3@0", "0.3@0", "0.3@0", "0.1@0"};
60321936Shselaskychar *f5[ENTS] = {"0.e@0", "0.98@0", "0.88@0", "0.f8@0", "0.58@0", "0.c@0", "0.18@0", "0.98@0", "0.18@0", "0.8@-1"};
61321936Shselasky
62321936Shselaskychar *f10[ENTS] = {"0.a7@0", "0.84c@0", "0.3c4@0", "0.5fc@0", "0.4ac@0", "0.5e@0", "0.e0c@0", "0.34c@0", "0.e8c@0", "0.a04@0"};
63321936Shselasky
64321936Shselaskychar *f15[ENTS] = {"0.538@-1", "0.3426@0", "0.e9e2@0", "0.4afe@0", "0.b256@0", "0.92f@0", "0.8f06@0", "0.f9a6@0", "0.b746@0", "0.8d02@0"};
65321936Shselaskychar *f16[ENTS] = {"0.29c@-1", "0.9a13@0", "0.74f1@0", "0.a57f@0", "0.d92b@0", "0.4978@0", "0.c783@0", "0.fcd3@0", "0.5ba3@0", "0.c681@0"};
66321936Shselaskychar *f17[ENTS] = {"0.28f@-1", "0.78bd@0", "0.2a7f8@0", "0.d19a8@0", "0.67b28@0", "0.2eb78@0", "0.2c1f8@0", "0.30c78@0", "0.de3@-1", "0.4c7f8@0"};
67321936Shselasky
68321936Shselaskychar *f31[ENTS] = {"0.75d9aa2a@0", "0.275d1d8c@0", "0.a319012@0", "0.103940ee@0", "0.e16e268@-1", "0.fdcf1ae2@0", "0.6474ec6c@0", "0.42459634@0", "0.33023282@0", "0.83fac0a@-1"};
69321936Shselaskychar *f32[ENTS] = {"0.baecd515@0", "0.13ae8ec6@0", "0.518c809@0", "0.881ca077@0", "0.870b7134@0", "0.7ee78d71@0", "0.323a7636@0", "0.a122cb1a@0", "0.99811941@0", "0.841fd605@0"};
70321936Shselaskychar *f33[ENTS] = {"0.fd7a665@-1", "0.aeb77c1d8@0", "0.484aff39@0", "0.db51effb@0", "0.58be5eee8@0", "0.b72904ea@0", "0.37b2d896@0", "0.249ddde3@0", "0.55f952ea8@0", "0.36a48d1e@0"};
71321936Shselasky
72321936Shselaskychar *f63[ENTS] = {"0.914e9e6cff4f6b9@0", "0.7753d3b8364c60ec@0", "0.3c15909cecf1c1f6@0", "0.2282cb02e5167c6a@0", "0.6d56c20a47e1e3ee@0", "0.7ca81d1d2b81d696@0", "0.8735c2c0afb793a6@0", "0.e69f64c1b64872a@0", "0.fa7462fdff85137e@0", "0.3b00603f67a341a2@0"};
73321936Shselaskychar *f64[ENTS] = {"0.48a74f367fa7b5c8@0", "0.bba9e9dc1b263076@0", "0.9e0ac84e7678e0fb@0", "0.11416581728b3e35@0", "0.b6ab610523f0f1f7@0", "0.be540e8e95c0eb4b@0", "0.439ae16057dbc9d3@0", "0.f34fb260db24395@0", "0.fd3a317effc289bf@0", "0.1d80301fb3d1a0d1@0"};
74321936Shselaskychar *f65[ENTS] = {"0.ffbbb886c236a4f8@0", "0.d8a08b80eb84f708@0", "0.18ffd40d1045b57a@0", "0.22331c6a18e9e3408@0", "0.6fae2b4eadd545aa8@0", "0.cbeccf54df9472d@0", "0.c8d6f04f6eca67d7@0", "0.ca5677d36ef2f0c68@0", "0.d7e08b3e2b1396c9@0", "0.68494ca6d39790378@0"};
75321936Shselasky
76321936Shselaskychar *f127[ENTS] = {"0.5ecd7526555eb140e3fb1e0e85533418@0", "0.e79f478cc99382ea6a0f94c1d8d7084a@0", "0.a7d40e994263bd8259ad171551c404f@0", "0.79eb5918686a65f14a7981d68eb03ee6@0", "0.a1823ab0d3c411543735462f7185a152@0", "0.16462c791250ec8e563de32c85d59c12@0", "0.913e9807a83f0ea1391ad9219cce9f2a@0", "0.5570e9192d54cec5d4326568993ae2c8@0", "0.13196ab23f80b5a635f7783acf721dba@0", "0.ef09177323fa17c6635b9e28aff78ce4@0"};
77321936Shselaskychar *f128[ENTS] = {"0.af66ba932aaf58a071fd8f0742a99a0c@0", "0.73cfa3c664c9c1753507ca60ec6b8425@0", "0.53ea074ca131dec12cd68b8aa8e20278@0", "0.3cf5ac8c343532f8a53cc0eb47581f73@0", "0.50c11d5869e208aa1b9aa317b8c2d0a9@0", "0.8b23163c892876472b1ef19642eace09@0", "0.489f4c03d41f87509c8d6c90ce674f95@0", "0.aab8748c96aa6762ea1932b44c9d7164@0", "0.98cb5591fc05ad31afbbc1d67b90edd@-1", "0.f7848bb991fd0be331adcf1457fbc672@0"};
78321936Shselasky
79321936Shselasky
80321936Shselaskystruct rt
81321936Shselasky{
82321936Shselasky  char **s;
83321936Shselasky  int nbits;
84321936Shselasky};
85321936Shselasky
86321936Shselaskystatic struct rt zarr[] =
87321936Shselasky{
88321936Shselasky  {z1, 1},
89321936Shselasky  {z2, 2},
90321936Shselasky  {z3, 3},
91321936Shselasky  {z4, 4},
92321936Shselasky  {z5, 5},
93321936Shselasky  {z10, 10},
94321936Shselasky  {z15, 15},
95321936Shselasky  {z16, 16},
96321936Shselasky  {z17, 17},
97321936Shselasky  {z31, 31},
98321936Shselasky  {z32, 32},
99321936Shselasky  {z33, 33},
100321936Shselasky  {z63, 63},
101321936Shselasky  {z64, 64},
102321936Shselasky  {z65, 65},
103321936Shselasky  {z127, 127},
104321936Shselasky  {z128, 128},
105321936Shselasky  {NULL, 0}
106321936Shselasky};
107321936Shselasky
108321936Shselaskystatic struct rt farr[] =
109321936Shselasky{
110321936Shselasky  {f1, 1},
111321936Shselasky  {f2, 2},
112321936Shselasky  {f3, 3},
113321936Shselasky  {f4, 4},
114321936Shselasky  {f5, 5},
115321936Shselasky  {f10, 10},
116321936Shselasky  {f15, 15},
117321936Shselasky  {f16, 16},
118321936Shselasky  {f17, 17},
119321936Shselasky  {f31, 31},
120321936Shselasky  {f32, 32},
121321936Shselasky  {f33, 33},
122321936Shselasky  {f63, 63},
123321936Shselasky  {f64, 64},
124321936Shselasky  {f65, 65},
125321936Shselasky  {f127, 127},
126321936Shselasky  {f128, 128},
127321936Shselasky  {NULL, 0}
128321936Shselasky};
129321936Shselasky
130321936Shselasky
131321936Shselaskyint
132321936Shselasky#if __STDC__
133321936Shselaskymain (int argc, char *argv[])
134321936Shselasky#else
135321936Shselaskymain (argc, argv)
136321936Shselasky     int argc;
137321936Shselasky     char *argv[];
138321936Shselasky#endif
139321936Shselasky{
140321936Shselasky  static char usage[] = "\
141321936Shselaskyusage: t-rand [function nbits]\n\
142321936Shselasky  function is one of z, f\n\
143321936Shselasky  nbits is number of bits\n\
144321936Shselasky";
145321936Shselasky  gmp_randstate_t rstate;
146321936Shselasky  mpz_t z, rz;
147321936Shselasky  mpf_t f, rf;
148321936Shselasky  enum { Z, F } func = Z;
149321936Shselasky  int nbits = 1;
150321936Shselasky  int verify_mode_flag = 1;
151321936Shselasky  register int i;
152321936Shselasky  struct rt *a;
153321936Shselasky
154321936Shselasky
155321936Shselasky  if (argc > 1)
156321936Shselasky    {
157321936Shselasky      if (argc < 3)
158321936Shselasky	{
159321936Shselasky	  fputs (usage, stderr);
160321936Shselasky	  exit (1);
161321936Shselasky	}
162321936Shselasky      verify_mode_flag = 0;
163321936Shselasky      if (*argv[1] == 'z')
164321936Shselasky	func = Z;
165321936Shselasky      if (*argv[1] == 'f')
166321936Shselasky	func = F;
167321936Shselasky      nbits = atoi (argv[2]);
168321936Shselasky    }
169321936Shselasky
170321936Shselasky  mpz_init (rz);
171321936Shselasky
172321936Shselasky  if (verify_mode_flag)
173321936Shselasky    {
174321936Shselasky#ifdef VERBOSE
175321936Shselasky      printf ("%s: verifying random numbers: ", argv[0]);
176321936Shselasky#endif
177321936Shselasky
178321936Shselasky      /* Test z.  */
179321936Shselasky      mpz_init (z);
180321936Shselasky      for (a = zarr; a->s != NULL; a++)
181321936Shselasky	{
182321936Shselasky	  gmp_randinit (rstate, GMP_RAND_ALG_LC, a->nbits);
183321936Shselasky	  if (gmp_errno != GMP_ERROR_NONE)
184321936Shselasky	    exit (1);
185321936Shselasky	  gmp_randseed_ui (rstate, SEED);
186321936Shselasky
187321936Shselasky	  for (i = 0; i < ENTS; i++)
188321936Shselasky	    {
189321936Shselasky	      mpz_urandomb (rz, rstate, a->nbits);
190321936Shselasky	      mpz_set_str (z, a->s[i], BASE);
191321936Shselasky	      if (mpz_cmp (z, rz) != 0)
192321936Shselasky		{
193321936Shselasky		  printf ("z%d: ", a->nbits);
194321936Shselasky		  mpz_out_str (stdout, BASE, rz);
195321936Shselasky		  printf (" should be ");
196321936Shselasky		  mpz_out_str (stdout, BASE, z);
197321936Shselasky		  puts ("");
198321936Shselasky		  exit (1);
199321936Shselasky		}
200321936Shselasky	    }
201321936Shselasky#ifdef VERBOSE
202321936Shselasky	  printf ("z%d ", a->nbits);
203321936Shselasky#endif
204321936Shselasky	  gmp_randclear (rstate);
205321936Shselasky	}
206321936Shselasky      mpz_clear (z);
207321936Shselasky
208321936Shselasky
209321936Shselasky      /* Test f.  */
210321936Shselasky      for (a = farr; a->s != NULL; a++)
211321936Shselasky	{
212321936Shselasky	  gmp_randinit (rstate, GMP_RAND_ALG_LC, a->nbits);
213321936Shselasky	  if (gmp_errno != GMP_ERROR_NONE)
214321936Shselasky	    exit (1);
215321936Shselasky	  gmp_randseed_ui (rstate, SEED);
216321936Shselasky
217321936Shselasky	  mpf_init2 (f, a->nbits);
218321936Shselasky	  mpf_init2 (rf, a->nbits);
219321936Shselasky	  for (i = 0; i < ENTS; i++)
220321936Shselasky	    {
221321936Shselasky	      mpf_urandomb (rf, rstate, a->nbits);
222321936Shselasky	      mpf_set_str (f, a->s[i], BASE);
223321936Shselasky	      if (mpf_cmp (f, rf) != 0)
224321936Shselasky		{
225321936Shselasky		  printf ("f%d: ", a->nbits);
226321936Shselasky		  mpf_out_str (stdout, BASE, a->nbits, rf);
227321936Shselasky		  printf (" should be ");
228321936Shselasky		  mpf_out_str (stdout, BASE, a->nbits, f);
229321936Shselasky		  puts ("");
230321936Shselasky		  exit (1);
231321936Shselasky		}
232321936Shselasky	    }
233321936Shselasky#ifdef VERBOSE
234321936Shselasky	  printf ("f%d ", a->nbits);
235321936Shselasky#endif
236321936Shselasky	  gmp_randclear (rstate);
237321936Shselasky	  mpf_clear (f);
238321936Shselasky	  mpf_clear (rf);
239321936Shselasky	}
240321936Shselasky
241321936Shselasky#ifdef VERBOSE
242321936Shselasky      puts ("");
243321936Shselasky#endif
244321936Shselasky    }
245321936Shselasky  else				/* Print mode.  */
246321936Shselasky    {
247321936Shselasky      gmp_randinit (rstate, GMP_RAND_ALG_LC, nbits);
248321936Shselasky      if (gmp_errno != GMP_ERROR_NONE)
249321936Shselasky	exit (1);
250321936Shselasky      gmp_randseed_ui (rstate, SEED);
251321936Shselasky
252321936Shselasky      switch (func)
253321936Shselasky	{
254321936Shselasky	case Z:
255321936Shselasky	  printf ("char *z%d[ENTS] = {", nbits);
256321936Shselasky	  for (i = 0; i < ENTS; i++)
257321936Shselasky	    {
258321936Shselasky	      mpz_urandomb (rz, rstate, nbits);
259321936Shselasky	      printf ("\"");
260321936Shselasky	      mpz_out_str (stdout, BASE, rz);
261321936Shselasky	      printf ("\"");
262321936Shselasky	      if (i != ENTS - 1)
263321936Shselasky		printf (", ");
264321936Shselasky	    }
265321936Shselasky	  printf ("};\n");
266321936Shselasky	  printf ("  {z%d, %d},\n", nbits, nbits);
267321936Shselasky	  break;
268321936Shselasky
269321936Shselasky	case F:
270321936Shselasky	  printf ("char *f%d[ENTS] = {", nbits);
271321936Shselasky	  mpf_init2 (rf, nbits);
272321936Shselasky	  for (i = 0; i < ENTS; i++)
273321936Shselasky	    {
274321936Shselasky	      mpf_urandomb (rf, rstate, nbits);
275321936Shselasky	      printf ("\"");
276321936Shselasky	      mpf_out_str (stdout, BASE, nbits, rf);
277321936Shselasky	      printf ("\"");
278321936Shselasky	      if (i != ENTS - 1)
279321936Shselasky		printf (", ");
280321936Shselasky	    }
281321936Shselasky	  printf ("};\n");
282321936Shselasky	  printf ("  {f%d, %d},\n", nbits, nbits);
283321936Shselasky	  mpf_clear (rf);
284321936Shselasky	  break;
285321936Shselasky
286321936Shselasky	default:
287321936Shselasky	  exit (1);
288321936Shselasky	}
289321936Shselasky
290321936Shselasky      gmp_randclear (rstate);
291321936Shselasky    }
292321936Shselasky
293321936Shselasky  mpz_clear (rz);
294321936Shselasky
295321936Shselasky  return 0;
296321936Shselasky}
297321936Shselasky