Karim Belabas on Wed, 28 Mar 2018 14:44:46 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Random bitstring generation in PARI library


* Aleksandr Lenin [2018-03-28 13:48]:
> 
> 
> On 03/27/2018 09:29 PM, Bill Allombert wrote:
> > On Tue, Mar 27, 2018 at 02:41:46PM +0300, Aleksandr Lenin wrote:
> >> Hello,
> >>
> >> what is the procedure to generate a random k-bit number, where k is
> >> greater than BITS_IN_LONG?
> > 
> > In GP, random(2^k)
> > 
> >> Reading through the manual
> >> (https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.10.0/libpari.pdf) I
> >> managed to find only pari_rand, random_bits, random_Fl which return an
> >> (unsigned) long, and I need a bigger number. The function randomi indeed
> >> returns a GEN, but in the range 1..p-1, but I need a random bitstring of
> >> length k.
> > 
> > In C you can do randomi(int2n(k))
> 
> this would return me a number in the range [0,2^k-1], this is not
> exactly what I need. I would like to be able to generate numbers of
> k-bit length only.

In GP:
  random([2^(k-1), 2^k-1])

In C:
  random(mkvec2(int2n(k-1), subiu(int2n(k),1)));

Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 21 23
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`