Using randomkit from Cython is stright forward. It is just one .c file to compile and one .h file to include. These are the files you need:
Then you e.g. have something like this in Cython (not debugged):
cdef extern from "randomkit.h":
ctypedef struct rk_state: pass
unsigned long rk_random(rk_state * state)
void rk_seed(unsigned long seed, rk_state * state)
from libc cimport stdlib
import os
cdef class RandomKit:
""" Simple randomkit wrapper """
cdef rk_state *state
cdef __cinit__(RandomKit self):
self.state = <rk_state*> stdlib.malloc(sizeof(rk_state))
if (self.state == NULL): raise MemoryError
cdef __init__(RandomKit self):
cdef unsigned long *seedptr
cdef object seed = os.urandom(sizeof(unsigned long))
seedptr = <unsigned long*>(<void*>(<char*> seed))
rk_seed(seedptr[0], self.state)
def __dealloc__(RandomKit self):
if self.state:
stdlib.free(self.state)
cdef unsigned long random(RandomKit self):
return rk_random(self.state)
Or just use rk_state locally in a function:
def foobar():
cdef rk_state state
cdef unsigned long *seedptr, random_value
cdef object seed = os.urandom(sizeof(unsigned long))
seedptr = <unsigned long*>(<void*>(<char*> seed))
rk_seed(seedptr[0], &state)
random_value = rk_random(&state)
(Look in randomkit.h if you need other distributions.)
Same thing when calling MKL or ACML random number generators from Cython. If you know how to call C from Cython, you just use them as you would in C. Just read the docs.
Note that randomkit, MKL and ACML uses the same numerical algorithm (Mersenne twister MT19937). Though the one in MKL and ACML might be more optimized (I haven't tested). This algorithm is very good for Monte Carlo simulations, but very bad for cryptography.
Sturla