Random Numbers and NTL

According to its website, the Number Theory Library (NTL) is a:

high-performance, portable C++ library providing data structures and algorithms for manipulating signed, arbitrary length integers, and for vectors, matrices, and polynomials over the integers and over finite fields.

I have been using it a lot because of the Cryptography class that I’m in this semester, and something that I need to do frequently is generate random numbers.

To do this, seeding is frequently required, and the NTL is no different, but it took a while for me to actually get it to work. The following will seed the random functions provided by the NTL ZZ class:

ZZ number, seed;
srand(getpid());
seed << ((long) GetTime() ^ rand());
SetSeed(seed);
RandomLen(number, rand() % 300);

Stick that at the top of your main() and you should be all set.

Also, does anyone know of a shorter, possibly easier to remember, way to do this? If so, the comments are open.

4 Responses to “Random Numbers and NTL”

  1. Jeff W
    December 8th, 2008 23:03
    1

    Ryan,

    I used random ZZ’s pretty extensively in the code for my honors thesis. Looking back at the code I see that all I did was this:

    srand ( time(NULL) );
    SetSeed( to_ZZ(rand()) );

    Then to get a random ZZ, I called:

    ZZ x = RandomBnd(N);

    which gives you 0 <= x <= N. Perhaps this isn’t the “most secure” or “random” way to do it, but it’s definitely easier and shorter.

    Good luck!
    Jeff

  2. Ryan Lewis
    December 9th, 2008 01:24
    2

    Cool! I knew there had to be an easier way, thanks for replying!

  3. bal
    January 6th, 2010 17:36
    3

    #include

    SetSeed(to_ZZ(clock()));

  4. bal
    January 6th, 2010 17:40
    4

    that should be include time.h

Leave a Reply