~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/tests/rand/gen.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2007-04-09 11:51:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070409115151-ql8cr0kalzx1jmla
Tags: 0.9i-20070324-2
Upload to unstable. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* gen.c -- Generate pseudorandom numbers. */
 
1
/* gen.c -- Generate pseudorandom numbers.
2
2
 
3
 
/*
4
 
Copyright 1999, 2000 Free Software Foundation, Inc.
 
3
Copyright 1999, 2000, 2002 Free Software Foundation, Inc.
5
4
 
6
5
This file is part of the GNU MP Library.
7
6
 
17
16
 
18
17
You should have received a copy of the GNU Lesser General Public License
19
18
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
20
 
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
 
MA 02111-1307, USA.
22
 
*/
 
19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
MA 02110-1301, USA. */
23
21
 
24
22
/* Examples:
25
23
 
26
 
  $ gen 10 
 
24
  $ gen 10
27
25
10 integers 0 <= X < 2^32 generated by mpz_urandomb()
28
26
 
29
27
  $ gen -f mpf_urandomb 10
37
35
 
38
36
  $ gen -s 1 10
39
37
10 integers, sequence seeded with 1
40
 
  
 
38
 
41
39
*/
42
40
 
43
41
#include <stdio.h>
67
65
    "  -a n     ASCII output in radix n (default, with n=10)\n" \
68
66
    "  -b       binary output\n" \
69
67
    "  -c a,c,m2exp use supplied LC scheme\n" \
70
 
    "  -C a,c,m     use supplied LC scheme\n" \
71
68
    "  -f func  random function, one of\n" \
72
69
    "           mpz_urandomb (default), mpz_urandomm, mpf_urandomb, rand, random\n" \
73
 
    "  -g alg   algorithm, one of lc (default), bbs\n" \
 
70
    "  -g alg   algorithm, one of mt (default), lc\n" \
74
71
    "  -h       print this text and exit\n" \
75
72
    "  -m n     maximum size of generated number plus 1 (0<= X < n) for mpz_urandomm\n" \
76
73
    "  -p       print used seed on stderr\n" \
77
 
    "  -q       quiet, no output\n" \
 
74
    "  -q       quiet, no output\n" \
78
75
    "  -s n     initial seed (default: output from time(3))\n" \
79
76
    "  -x f,t   exclude all numbers f <= x <= t\n" \
80
77
    "  -z n     size in bits of generated numbers (0<= X <2^n) (default 32)\n" \
103
100
  char *str_a, *str_adder, *str_m;
104
101
  mpz_t z_a, z_m, z_mmax;
105
102
  unsigned long int ul_adder;
106
 
  
107
 
  enum 
 
103
 
 
104
  enum
108
105
  {
109
106
    RFUNC_mpz_urandomb = 0,
110
107
    RFUNC_mpz_urandomm,
114
111
  } rfunc = RFUNC_mpz_urandomb;
115
112
  char *rfunc_str[] =  { "mpz_urandomb", "mpz_urandomm", "mpf_urandomb",
116
113
                         "rand", "random" };
117
 
  gmp_randalg_t ralg = GMP_RAND_ALG_DEFAULT;
118
 
  char *ralg_str[] = { "lc", "bbs" };
 
114
  enum
 
115
  {
 
116
    RNG_MT = 0,
 
117
    RNG_LC
 
118
  };
 
119
  gmp_randalg_t ralg = RNG_MT;
 
120
  /* Texts for the algorithms.  The index of each must match the
 
121
     corresponding algorithm in the enum above.  */
 
122
  char *ralg_str[] = { "mt", "lc" };
119
123
 
120
124
  mpf_init (f_xf);
121
125
  mpf_init (f_xt);
125
129
  mpz_init_set_ui (z_mmax, 0);
126
130
 
127
131
 
128
 
  while ((c = getopt (argc, argv, "a:bc:C:f:g:hm:n:pqs:z:x:")) != -1)
 
132
  while ((c = getopt (argc, argv, "a:bc:f:g:hm:n:pqs:z:x:")) != -1)
129
133
    switch (c)
130
134
      {
131
135
      case 'a':
140
144
        break;
141
145
 
142
146
      case 'c':                 /* User supplied LC scheme: a,c,m2exp */
143
 
      case 'C':                 /* User supplied LC scheme: a,c,m */
144
147
        if (NULL == (str_a = strtok (optarg, ","))
145
148
            || NULL == (str_adder = strtok (NULL, ","))
146
149
            || NULL == (str_m = strtok (NULL, ",")))
155
158
#else
156
159
        ul_adder = (unsigned long int) atoi (str_adder);
157
160
#endif
158
 
 
 
161
        
159
162
        if (mpz_init_set_str (z_a, str_a, 0))
160
163
          {
161
164
            fprintf (stderr, "gen: bad LC scheme parameter `a': %s\n", str_a);
167
170
                     str_adder);
168
171
            exit (1);
169
172
          }
170
 
        if (c == 'c')
171
 
          m2exp = atol (str_m);
172
 
        else
173
 
          mpz_init_set_str (z_m, str_m, 0);
 
173
        m2exp = atol (str_m);
174
174
 
175
175
        lc_scheme_from_user = 1;
176
176
        break;
272
272
    }
273
273
 
274
274
  mpf_set_prec (f1, size);
275
 
  
 
275
 
276
276
  /* init random state and plant seed */
277
277
  switch (rfunc)
278
278
    {
284
284
#endif
285
285
    case RFUNC_mpz_urandomb:
286
286
    case RFUNC_mpz_urandomm:
287
 
      if (! lc_scheme_from_user)
288
 
        {
289
 
          gmp_randinit (rstate, ralg, MIN (128, size));
290
 
        }
291
 
      else
292
 
        {
293
 
          if (m2exp != 0)
 
287
      switch (ralg)
 
288
        {
 
289
        case RNG_MT:
 
290
          gmp_randinit_mt (rstate);
 
291
          break;
 
292
 
 
293
        case RNG_LC:
 
294
          if (! lc_scheme_from_user)
 
295
            gmp_randinit_lc_2exp_size (rstate, MIN (128, size));
 
296
          else
294
297
            gmp_randinit_lc_2exp (rstate, z_a, ul_adder, m2exp);
295
 
          else
296
 
            gmp_randinit_lc (rstate, z_a, ul_adder, z_m);
297
 
        }
298
 
      
299
 
      if (gmp_errno != GMP_ERROR_NONE)
300
 
        {
301
 
          if (gmp_errno & GMP_ERROR_INVALID_ARGUMENT)
302
 
            fprintf (stderr, "gen: asking for too big random state\n");
303
 
          if (gmp_errno & GMP_ERROR_UNSUPPORTED_ARGUMENT)
304
 
            fprintf (stderr, "gen: unsupported algorithm\n");
 
298
          break;
 
299
 
 
300
        default:
 
301
          fprintf (stderr, "gen: unsupported algorithm\n");
305
302
          exit (1);
306
303
        }
 
304
 
307
305
      gmp_randseed (rstate, z_seed);
308
306
      break;
309
307
 
319
317
        srandomdev ();
320
318
#else
321
319
      fprintf (stderr, "gen: unsupported algorithm\n");
322
 
#endif      
 
320
#endif
323
321
      break;
324
322
 
325
323
    default:
332
330
    switch (rfunc)
333
331
      {
334
332
      case RFUNC_mpf_urandomb:
335
 
      
 
333
 
336
334
        if (mpf_set_str (f_xf, str_xf, 10) ||
337
335
            mpf_set_str (f_xt, str_xt, 10))
338
336
          {
347
345
        fprintf (stderr, "gen: exclusion not implemented for chosen " \
348
346
                 "randomization function.  all numbers included in sequence.\n");
349
347
      }
350
 
  
 
348
 
351
349
  /* generate and print */
352
350
  if (argc > 0)
353
351
    {
459
457
          fprintf (stderr, "gen: random function not implemented\n");
460
458
          exit (1);
461
459
        }
462
 
      
 
460
 
463
461
    }
464
462
 
465
463
  /* clean up */