[bigint] Random Generator

Nicolás Carrasco <ncarrasco2006 at gmail.com>
Wed Jun 23 07:39:16 PDT 2010


Hi Matt,

For random generation I am using this dirty generatator.
Do you think it is ok?

void RandomGenerator::SetTimeSeed()
{
    srand ( time(NULL) );
}

BigUnsigned RandomGenerator::GeneratePseudoRandomNumber(const BigUnsigned&
min, const BigUnsigned& max)
{
    const int arrayLength = 34;
    unsigned long array[arrayLength];

    for (int i=0; i <arrayLength; i++ )
    {
        unsigned long low = rand();
        unsigned long high = rand();

        low &= 0x0000FFFF;
        high &= 0x0000FFFF;
        high = high << 16;

        int tam = sizeof(unsigned long);
        if (tam == 8) // if long is 64 bits instead of 32 as int
        {
            unsigned long upper_low = rand();
            unsigned long upper_high = rand();
            upper_low &= 0x0000FFFF;
            upper_high &= 0x0000FFFF;

            upper_low = upper_low << 32;
            upper_high = upper_high << 48;

            array[i] |= low;
            array[i] |= high;
            array[i] |= upper_low;
            array[i] |= upper_high;

        }
        else
        {
            array[i] = high | low;
        }



    }

    BigUnsigned result(array,  arrayLength);

    result = (result % (max-min +1) ) + min;

    return result;
}


Thanks,
Nicolas.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mattmccutchen.net/mailman/archives/bigint/attachments/20100623/0cdb5c25/attachment.htm>


More information about the bigint mailing list