Hi all,
I'm having some difficulty understanding my options for seeding the
base generators in mersenne_twister.hpp. I'm interested in the array
initialization that is somewhat obtusely mentioned in the
documentation. In boost/random/mersenne_twister.hpp I see the
following relevant items:
template<class It> mersenne_twister(It& first, It last) {
seed(first,last); }
...
template<class It>
void seed(It& first, It last)
{
int j;
for(j = 0; j < n && first != last; ++j, ++first)
x[j] = *first;
i = n;
if(first == last && j < n)
throw std::invalid_argument("mersenne_twister::seed");
}
I gather that "It" is some kind of iterator, and the natural candidate
would be something like std::vector::iterator, but not having
truly come to terms with templates, it is unclear to me how to actually
call this. The following minimal code snippet fails to compile with
g++ (gcc version 3.3 20030304 (Apple Computer, Inc. build 1671)).
Errors below.
#include <iostream>
#include <vector>
#include
typedef boost::mt19937 Mt19937BaseGenerator; // base generator
typedef boost::uniform_real<> UniformReal; // distribution
// variate generator
typedef boost::variate_generator
Mt19937UniformRealGenerator;
int main() {
std::vector seedVector(4);
seedVector[0] = 12345u; // make up some seeds
seedVector[1] = 98765u;
seedVector[2] = 12942u;
seedVector[3] = 20343u;
// attempt to seed with vector iterators
Mt19937BaseGenerator mt19937Generator(seedVector.begin(),
seedVector.end());
UniformReal distribution(0, 1);
Mt19937UniformRealGenerator myGen(mt19937Generator, distribution);
for (unsigned i = 0; i < 100; ++i) {
std::cout << myGen() << std::endl;
}
return 0;
}
mt19937Seed.cpp: In function `int main()':
mt19937Seed.cpp:18: error: no matching function for call to `
boost::random::mersenne_twister::mersenne_twister(
__gnu_cxx::__normal_iterator > >, __gnu_cxx::__normal_iterator > >)'
/sw/include/boost/random/mersenne_twister.hpp:43: error: candidates are:
boost::random::mersenne_twister::mersenne_twister(const
boost::random::mersenne_twister&)
/sw/include/boost/random/mersenne_twister.hpp:69: error:
boost::random::mersenne_twister::mersenne_twister(It&, It) [with It =
__gnu_cxx::__normal_iterator > >, UIntType = uint32_t, int w = 32, int n
= 624,
int m = 397, int r = 31, UIntType a = 0x09908b0df, int u = 11, int s
= 7,
UIntType b = 0x09d2c5680, int t = 15, UIntType c = 0x0efc60000, int
l = 18,
UIntType val = 0x0c77666de]
/sw/include/boost/random/mersenne_twister.hpp:68: error:
boost::random::mersenne_twister::mersenne_twister(UIntType) [with UIntType = uint32_t, int w =
32, int
n = 624, int m = 397, int r = 31, UIntType a = 0x09908b0df, int u =
11, int
s = 7, UIntType b = 0x09d2c5680, int t = 15, UIntType c =
0x0efc60000, int l
= 18, UIntType val = 0x0c77666de]
/sw/include/boost/random/mersenne_twister.hpp:60: error:
boost::random::mersenne_twister::mersenne_twister() [with UIntType = uint32_t, int w = 32, int
n = 624,
int m = 397, int r = 31, UIntType a = 0x09908b0df, int u = 11, int s
= 7,
UIntType b = 0x09d2c5680, int t = 15, UIntType c = 0x0efc60000, int
l = 18,
UIntType val = 0x0c77666de]
Any help? I always have a really hard time interpreting
template-related compiler errors.
Thanks,
Matt
--
Matt Holland
Population Biology Graduate Group
University of California
Davis, CA 95616