Opened 8 years ago

Last modified 8 years ago

#10921 new Bugs

Seeding boost::random::mt19937 from a pair of iterators throws an exception

Reported by: Adam Romanek <romanek.adam@…> Owned by: No-Maintainer
Milestone: To Be Determined Component: random
Version: Boost 1.57.0 Severity: Problem
Keywords: Cc:

Description

The problem is that when seeding boost::random::mt19937 from a pair of iterators std::invalid_argument("Not enough elements in call to seed") exception is thrown.

An isolated test case looks like this:

#include <boost/array.hpp>
#include <boost/random.hpp>

//typedef boost::random::rand48 generator_t;
typedef boost::random::mt19937 generator_t;

int main() {
  typedef boost::array<int, 3> seed_range_t;
  seed_range_t seed = {1, 2, 3};

  boost::random::seed_seq seed2(seed.begin(), seed.end());
  generator_t generator1(seed2); // works fine

  seed_range_t::iterator begin = seed.begin();
  generator_t generator2(begin, seed.end()); // throws std::invalid_argument("Not enough elements in call to seed")
}

The documentation of the relevant constructor [1] does not help too much.

Please note that using the same input sequence through boost::random::seed_seq works fine. Moreover, the same input sequence passed directly to the constructor of boost::random::rand48 generator also produces no exceptions.

I'm not sure if mersenne_twister_engine needs larger input or the implementation is broken. Please advice.

[1] http://www.boost.org/doc/libs/1_57_0/doc/html/boost/random/mersenne_twister_engine.html#idp93974544-bb

Change History (2)

comment:1 by Steven Watanabe, 8 years ago

This behavior is expected. The iterator constructors require you to provide a range sufficiently large to fill the entire state of the generator. Seeding with a pair of iterators is provided for backwards compatibility. New code should use seed_seq (or std::seed_seq).

comment:2 by Adam Romanek <romanek.adam@…>, 8 years ago

So this is a shortcoming of the documentation. Thanks for the info.

Note: See TracTickets for help on using tickets.