Boost logo

Boost Users :

From: chun ping wang (cablepuff_at_[hidden])
Date: 2006-08-09 15:10:20


Hi, I have a question how would i use boost to generate a 160 bit random
integer.

Heres the random number generator i wrote thanks to some of the boost
community help.

#ifndef RANDOM_NUMBER_HPP
#define RANDOM_NUMBER_HPP
#include <boost/random/shuffle_output.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#include <Windows.h>
#ifdef _linux_
   #include <boost/nondet_random.hpp>
#endif
#include <boost/cast.hpp>

// credit and thanks to Andrew Holden for help on this.

namespace Random
{
       typedef boost::kreutzer1986 base_generator_type;
       // typedef boost::mt19937 base_generator_type;
       typedef boost::uniform_int<> distribution_type;
       typedef boost::variate_generator<base_generator_type&,
distribution_type> gen_type;

       // boost::random_device nondet_generator;

       //Declare the generator as a global variable for easy reuse
       #ifdef __linux__
              boost::random_device dev;
              base_generator_type generator(dev());
       #else
              base_generator_type generator(GetTickCount());
       #endif

       /***************************************************
        * initializes the random number generator *
        **************************************************/
       void initialize()
       {
           for (int i = 0; i < 900; ++i)
           {
              #ifdef __linux__
                   generator.seed(dev());
              #else
                   generator.seed(GetTickCount());
              #endif
           }
       }

       /***************************************************
        * generates a random number between a and b *
        * @param a the starting range *
        * @param b the integer ending range *
        * @return N such that a <= N <= b *
        **************************************************/
       template <class T>
       T randint (const T& a, const T& b)
       {
               //Check the parameters
               BOOST_STATIC_ASSERT(boost::is_integral<T>::value);
               assert (b >= a);

               //Set up the desired distribution
               gen_type ran_gen(generator, distribution_type(a, b));

               //Get a random number
               return ran_gen();
       }
}

#endif /* RANDOM_NUMBER_HPP */

Heres a code to generate random prime number.

/***************************************************
* n <- random number with num_digits digits *
* if n is even then n = n + 1; *
* while is_prime(n, 150) *
* { *
* n = n + 2; *
* } *
* return n; *
***************************************************/
template <typename T>
T boost::random_prime(boost::function<bool(const T&, const T&)> f, const
unsigned int& num_digits)
{
   BOOST_STATIC_ASSERT(is_integral<T>::value);
   T n(Random::randint<T>(numeric_cast<T>(pow(2.0, num_digits - 1)),
                          numeric_cast<T>(pow(2.0, num_digits)))
                          );
   if (n%2 == 0)
   {
        ++n;
   }
   while (f(n, 20))
   {
      n += 2;
   }
   return n;
}

now num_digits is number of bits. If i pass 160 bit as like for DSA
algorithm the program crashes. I pass 8 and it works, how come? Is there a
better way to do this? Oh f is any function that can deteremine if a number
is most likely a prime or not....

Thanks.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net