[Numpy-discussion] Adopt Mersenne Twister 64bit?

Neal Becker ndbecker2 at gmail.com
Wed Mar 13 09:23:59 EDT 2013

Neal Becker wrote:

> Neal Becker wrote:
>> I guess I talked to you about 100 years ago about sharing state between numpy
>> rng and code I have in c++ that wraps boost::random.  So is there a C-api for
>> this RandomState object I could use to call from c++?  Maybe I could do
>> something with that.
>> The c++ code could invoke via the python api, but that might be slower.  I'm
>> just rambling here, I'd have to see the API to get some ideas.
> I think if I could just grab a long int from the underlying mersenne twister,
> through some c api?

Well, this at least appears to work - probably not the most efficient approach - 
calls the RandomState object via the python interface to get 4 bytes at a time:

int test1 (bp::object & rs) {
  bp::str bytes = call_method<bp::str> (rs.ptr(), "bytes", 4); // get 4 bytes
  return *reinterpret_cast<int*> (PyString_AS_STRING (bytes.ptr()));

BOOST_PYTHON_MODULE (numpy_rand) {

  def ("test1", &test1);

More information about the NumPy-Discussion mailing list