version 1.1.1.2, 2000/09/09 14:12:56 |
version 1.1.1.3, 2003/08/25 16:06:33 |
|
|
/* mpz_random -- Generate a random mpz_t of specified size. |
/* mpz_random -- Generate a random mpz_t of specified size in limbs. |
This function is non-portable and generates poor random numbers. |
|
|
|
Copyright (C) 1991, 1993, 1994, 1995 Free Software Foundation, Inc. |
Copyright 2001 Free Software Foundation, Inc. |
|
|
This file is part of the GNU MP Library. |
This file is part of the GNU MP Library. |
|
|
Line 22 MA 02111-1307, USA. */ |
|
Line 21 MA 02111-1307, USA. */ |
|
|
|
#include "gmp.h" |
#include "gmp.h" |
#include "gmp-impl.h" |
#include "gmp-impl.h" |
#include "urandom.h" |
|
|
|
void |
void |
#if __STDC__ |
|
mpz_random (mpz_ptr x, mp_size_t size) |
mpz_random (mpz_ptr x, mp_size_t size) |
#else |
|
mpz_random (x, size) |
|
mpz_ptr x; |
|
mp_size_t size; |
|
#endif |
|
{ |
{ |
mp_size_t i; |
mpz_urandomb (x, RANDS, ABS (size) * BITS_PER_MP_LIMB); |
mp_limb_t ran; |
if (size < 0) |
mp_ptr xp; |
SIZ(x) = -SIZ(x); |
mp_size_t abs_size; |
|
|
|
abs_size = ABS (size); |
|
|
|
if (x->_mp_alloc < abs_size) |
|
_mpz_realloc (x, abs_size); |
|
|
|
xp = x->_mp_d; |
|
|
|
for (i = 0; i < abs_size; i++) |
|
{ |
|
ran = urandom (); |
|
xp[i] = ran; |
|
} |
|
|
|
MPN_NORMALIZE (xp, abs_size); |
|
x->_mp_size = size < 0 ? -abs_size : abs_size; |
|
} |
} |