Home | History | Annotate | Line # | Download | only in mpz
rrandomb.c revision 1.1.1.1
      1 /* mpz_rrandomb -- Generate a positive random mpz_t of specified bit size, with
      2    long runs of consecutive ones and zeros in the binary representation.
      3    Meant for testing of other MP routines.
      4 
      5 Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
      6 
      7 This file is part of the GNU MP Library.
      8 
      9 The GNU MP Library is free software; you can redistribute it and/or modify
     10 it under the terms of the GNU Lesser General Public License as published by
     11 the Free Software Foundation; either version 3 of the License, or (at your
     12 option) any later version.
     13 
     14 The GNU MP Library is distributed in the hope that it will be useful, but
     15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     17 License for more details.
     18 
     19 You should have received a copy of the GNU Lesser General Public License
     20 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
     21 
     22 #include "gmp.h"
     23 #include "gmp-impl.h"
     24 
     25 static void gmp_rrandomb __GMP_PROTO ((mp_ptr, gmp_randstate_t, mp_bitcnt_t));
     26 
     27 void
     28 mpz_rrandomb (mpz_ptr x, gmp_randstate_t rstate, mp_bitcnt_t nbits)
     29 {
     30   mp_size_t nl;
     31 
     32   nl = (nbits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS;
     33   if (nbits != 0)
     34     {
     35       MPZ_REALLOC (x, nl);
     36       gmp_rrandomb (PTR(x), rstate, nbits);
     37     }
     38 
     39   SIZ(x) = nl;
     40 }
     41 
     42 /* Ask _gmp_rand for 32 bits per call unless that's more than a limb can hold.
     43    Thus, we get the same random number sequence in the common cases.
     44    FIXME: We should always generate the same random number sequence!  */
     45 #if GMP_NUMB_BITS < 32
     46 #define BITS_PER_RANDCALL GMP_NUMB_BITS
     47 #else
     48 #define BITS_PER_RANDCALL 32
     49 #endif
     50 
     51 static void
     52 gmp_rrandomb (mp_ptr rp, gmp_randstate_t rstate, mp_bitcnt_t nbits)
     53 {
     54   mp_bitcnt_t bi;
     55   mp_limb_t ranm;		/* buffer for random bits */
     56   unsigned cap_chunksize, chunksize;
     57   mp_size_t i;
     58 
     59   /* Set entire result to 111..1  */
     60   i = (nbits + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS - 1;
     61   rp[i] = GMP_NUMB_MAX >> (GMP_NUMB_BITS - (nbits % GMP_NUMB_BITS)) % GMP_NUMB_BITS;
     62   for (i = i - 1; i >= 0; i--)
     63     rp[i] = GMP_NUMB_MAX;
     64 
     65   _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
     66   cap_chunksize = nbits / (ranm % 4 + 1);
     67   cap_chunksize += cap_chunksize == 0; /* make it at least 1 */
     68 
     69   bi = nbits;
     70 
     71   for (;;)
     72     {
     73       _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
     74       chunksize = 1 + ranm % cap_chunksize;
     75       bi = (bi < chunksize) ? 0 : bi - chunksize;
     76 
     77       if (bi == 0)
     78 	break;			/* low chunk is ...1 */
     79 
     80       rp[bi / GMP_NUMB_BITS] ^= CNST_LIMB (1) << bi % GMP_NUMB_BITS;
     81 
     82       _gmp_rand (&ranm, rstate, BITS_PER_RANDCALL);
     83       chunksize = 1 + ranm % cap_chunksize;
     84       bi = (bi < chunksize) ? 0 : bi - chunksize;
     85 
     86       mpn_incr_u (rp + bi / GMP_NUMB_BITS, CNST_LIMB (1) << bi % GMP_NUMB_BITS);
     87 
     88       if (bi == 0)
     89 	break;			/* low chunk is ...0 */
     90     }
     91 }
     92