Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict between uuid.uuid() from Python and std::rand() from C++

My soft is written in C++ and called by python scripts (through Swig). When the python function uuid.uuid1() is called in the scripts, the seed used by std::rand() of C++ seems lost. It's a problem beacause I have to be able to relaunch my soft with exactly the same behaviour in the C++ code (it's not a matter for uniqid's).

The problem is simplified in the 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 is done with the 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 I launch the following python testcase several times, I can see that the first number is always the same (as expected), but the second number, generated after the call of uuid.uuid1() changes at 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 you any idea how I can use python uuid without killing my C++ seed ? Thanks in advance. (EDIT : my configuration : Linux openSUSE 12.3, 64 bits, python 2.7.3 (but same problem with 2.7.2), swig 2.0.9, gcc 4.7.2 (but same problem with 4.5.1))

like image 622
Caduchon Avatar asked Nov 10 '22 10:11

Caduchon


1 Answers

I found the code of uuid here : http://pythoninside.com/en/source-code/2.7.5/uuid/uuid.py and I copied and used the code in the example script (i.e. without import uuid). The problem comes from the call _uuid_generate_time(_buffer) at line 500. This function is defined as an alias of ctypes.CDLL(ctypes.util.find_library('uuid')).uuid_generate_time at lines 402-410. This is the only mention of the bug I found : https://savannah.cern.ch/bugs/?24456

like image 105
Caduchon Avatar answered Nov 15 '22 07:11

Caduchon