Conflict between uuid.uuid() from Python and std::rand() from C++ -
my soft written in c++ , called python scripts (through swig). when python function uuid.uuid1() called in scripts, seed used std::rand() of c++ seems lost. it's problem beacause have able relaunch soft same behaviour in c++ code (it's not matter uniqid's).
the problem simplified in following example :
c++ file testrand.h :
#ifndef __include__testrand_h__ #define __include__testrand_h__ void initialize(unsigned long int seed); unsigned long int get_number(); #endif
c++ file testrand.cpp :
#include "testrand.h" #include <cstdlib> void initialize(unsigned long int seed) { std::srand(seed); } unsigned long int get_number() { return std::rand(); }
swig file testrand.i :
%module testrand %{ #include "testrand.h" %} %include "testrand.h"
the compilation done following command :
swig -python -c++ testrand.i g++ -c -fpic testrand.cpp testrand_wrap.cxx -i/usr/include/python2.7/ g++ -shared testrand.o testrand_wrap.o -o _testrand.so
if launch following python testcase several times, can see first number same (as expected), second number, generated after call of uuid.uuid1() changes @ each run.
import testrand import uuid testrand.initialize(10) x1 = testrand.get_number() print x1 uuid.uuid1() x2 = testrand.get_number() print x2
several runs :
> python testcase.py 1215069295 1691632206 > python testcase.py 1215069295 746144017 > python testcase.py 1215069295 377602282
have idea how can use python uuid without killing c++ seed ? in advance. (edit : configuration : linux opensuse 12.3, 64 bits, python 2.7.3 (but same problem 2.7.2), swig 2.0.9, gcc 4.7.2 (but same problem 4.5.1))
i found code of uuid here : http://pythoninside.com/en/source-code/2.7.5/uuid/uuid.py , copied , used code in example script (i.e. without import uuid). problem comes call _uuid_generate_time(_buffer)
@ line 500. function defined alias of ctypes.cdll(ctypes.util.find_library('uuid')).uuid_generate_time
@ lines 402-410. mention of bug found : https://savannah.cern.ch/bugs/?24456
Comments
Post a Comment