[Numpy-discussion] numarray.random_array number generation in C code

Faheem Mitha faheem at email.unc.edu
Fri Oct 1 22:28:41 CDT 2004

Dear People,

I want to write some C++ code to link with Python, using the 
Boost.Python interface. I need to generate random numbers in the C++ 
code, and I was wondering as to the best way of doing this.

Note that it is important that the random number generation interoperate 
seamlessly with Python, in the sense that the behavior of the calls to 
the RNG is the same whether calls are made at the C level or the Python 
level. I hope the reasons why this is important are obvious.

I was thinking that the method should go like this.

1) When C/C++ code called, reads seed from python random state.

2) Does its stuff.

3) Writes seed back to python level when it exits.

After doing a little investigation of the numarray.random_array python 
library and associated extension modules, it seems possible that the 
answer is simpler than I had supposed. However, I would appreciate it if 
someone would tell me if my understanding is incorrect in some places.

Summary: It seems that I can just call all the C entry point routines 
defined in ranlib.h, without worrying about getting or setting seeds.


The structure of this random number facility has three parts, all files in 

1) low-level C routines: Packages/RandomArray2/Src/com.c and 

com.c: basic RNG stuff; getting and setting seeds etc.
ranlib.c: Random number generator algorithms for different distributions 

2) Python to C interface: Packages/RandomArray2/Src/ranlibmodule.c.

This interfaces the stuff in com.c and ranlib.c.

3) Python wrapper: Packages/RandomArray2/Lib/RandomArray2.py.

This wraps the C interface. In most cases it does not do much else besides 
some basic argument error checking.

More information about the Numpy-discussion mailing list