1 /* mpz_random2 -- Generate a positive random MP_INT of specified size, with
2 long runs of consecutive ones and zeros in the binary representation.
3 Meant for testing of other MP routines.
5 Copyright (C) 1991, 1993 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 The GNU MP Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with the GNU MP Library; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #if !defined(__GNUC__)
27 #define __inline__ inline
30 #if defined (hpux) || defined (__alpha__)
31 /* HPUX lacks random(). DEC Alpha's random() returns a double. */
32 static __inline__ long
43 mpz_random2 (MP_INT *x, mp_size size)
52 mp_size xsize, abs_size;
55 abs_size = ABS (size);
59 if (x->alloc < abs_size)
60 _mpz_realloc (x, abs_size);
67 n_bits = (ran >> 1) % BITS_PER_MP_LIMB;
71 if (xsize == abs_size)
76 /* Would we get a too large result in mpn_lshift? */
78 && (xp[xsize - 1] >> (BITS_PER_MP_LIMB - n_bits)) != 0)
81 cy_limb = mpn_lshift (xp, xp, xsize, n_bits);
83 xp[xsize++] = cy_limb;
86 xp[0] |= ((mp_limb) 1 << n_bits) - 1;