Boost logo

Boost Users :

Subject: [Boost-users] Simple question about boost::random
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-06-13 17:35:30


I think I'm overlooking something very simple, but I'm trying to
generate random numbers that are uniform over the open range of
various data types. So I wrote this code:

template<typename T> struct rand_type_range
{
        typedef boost::numeric::bounds<T> bounds;

        static T generate()
        {
                static boost::uniform_int<T> range(bounds::lowest()+1, bounds::highest()-1);
                static boost::variate_generator<boost::minstd_rand&,
boost::uniform_int<T> > generate(rng, range);

                return generate();
        }
};

Using this with anything other than a boost::uint32_t gives lots of
warnings, although the results turn out correct. Looking at the
warnings it seems the infrastructure is still trying to use
boost::uint32_t's for everything, so am I missing something simple?

1>Q:\include\boost\1.39.0\boost/random/uniform_int.hpp(135) : warning
C4244: '+=' : conversion from 'unsigned long' to 'unsigned short',
possible loss of data
1> Q:\include\boost\1.39.0\boost/random/uniform_int.hpp(64) :
see reference to function template instantiation 'unsigned short
boost::uniform_int<IntType>::generate<Engine>(Engine &,unsigned
short,unsigned short,unsigned short)' being compiled
1> with
1> [
1> IntType=boost::uint16_t,
1> Engine=boost::random::detail::pass_through_engine<boost::random::detail::pass_through_engine<boost::random::linear_congruential<boost::int32_t,48271,0,2147483647,399268537>
>>
1> ]
1> Q:\include\boost\1.39.0\boost/random/variate_generator.hpp(100)
: see reference to function template instantiation 'unsigned short
boost::uniform_int<IntType>::operator
()<boost::random::detail::pass_through_engine<UniformRandomNumberGenerator>>(Engine
&)' being compiled
1> with
1> [
1> IntType=boost::uint16_t,
1> UniformRandomNumberGenerator=boost::random::detail::pass_through_engine<boost::random::linear_congruential<boost::int32_t,48271,0,2147483647,399268537>
>,
1> Engine=boost::random::detail::pass_through_engine<boost::random::detail::pass_through_engine<boost::random::linear_congruential<boost::int32_t,48271,0,2147483647,399268537>
>>
1> ]
1> Q:\include\boost\1.39.0\boost/random/variate_generator.hpp(100)
: while compiling class template member function 'unsigned short
boost::variate_generator<Engine,Distribution>::operator ()(void)'
1> with
1> [
1> Engine=boost::minstd_rand &,
1> Distribution=boost::uniform_int<boost::uint16_t>
1> ]
1> c:\dev\agent\windows\tests\bit_search.hpp(54) : see
reference to class template instantiation
'boost::variate_generator<Engine,Distribution>' being compiled
1> with
1> [
1> Engine=boost::minstd_rand &,
1> Distribution=boost::uniform_int<boost::uint16_t>
1> ]
1> c:\dev\agent\windows\tests\bit_search.hpp(52) : while
compiling class template member function 'boost::uint16_t
rand_type_value<T>::generate(void)'
1> with
1> [
1> T=boost::uint16_t
1> ]
1> c:\dev\agent\windows\tests\bit_search.hpp(62) : see
reference to class template instantiation 'rand_type_value<T>' being
compiled
1> with
1> [
1> T=boost::uint16_t
1> ]
1>Q:\include\boost\1.39.0\boost/random/detail/signed_unsigned_tools.hpp(64)
: warning C4244: 'return' : conversion from 'base_unsigned' to
'unsigned short', possible loss of data
1> Q:\include\boost\1.39.0\boost/random/detail/signed_unsigned_tools.hpp(64)
: while compiling class template member function 'unsigned short
boost::random::detail::add<T1,T2>::operator ()(T1,T2)'
1> with
1> [
1> T1=base_unsigned,
1> T2=unsigned short
1> ]
1> Q:\include\boost\1.39.0\boost/random/uniform_int.hpp(116) :
see reference to class template instantiation
'boost::random::detail::add<T1,T2>' being compiled
1> with
1> [
1> T1=base_unsigned,
1> T2=unsigned short
1> ]
1> Q:\include\boost\1.39.0\boost/random/uniform_int.hpp(64) :
see reference to function template instantiation 'unsigned short
boost::uniform_int<IntType>::generate<Engine>(Engine &,unsigned
short,unsigned short,unsigned short)' being compiled
1> with
1> [
1> IntType=boost::uint16_t,
1> Engine=boost::random::detail::pass_through_engine<boost::random::detail::pass_through_engine<boost::random::linear_congruential<boost::int32_t,48271,0,2147483647,399268537>
>>
1> ]
1>Q:\include\boost\1.39.0\boost/random/uniform_smallint.hpp(79) :
warning C4244: 'return' : conversion from 'long' to 'unsigned short',
possible loss of data
1> Q:\include\boost\1.39.0\boost/random/uniform_int.hpp(150) :
see reference to function template instantiation 'unsigned short
boost::uniform_smallint<IntType>::operator ()<Engine>(Engine &)' being
compiled
1> with
1> [
1> IntType=unsigned short,
1> Engine=boost::random::detail::pass_through_engine<boost::random::detail::pass_through_engine<boost::random::linear_congruential<boost::int32_t,48271,0,2147483647,399268537>
>>
1> ]
1>Linking...
1>LINK : c:\dev\agent\windows\tests\debug-winxp\Win32\bin\tests.exe
not found or not built by the last incremental link; performing full
link


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