3
0
mirror of https://github.com/triqs/dft_tools synced 2024-10-31 19:23:45 +01:00
dft_tools/pytriqs/random/random_generator.pyx
Michel Ferrero 2921cbcc06 Cythonize random generators
A trivial cythonized version of the c++ random_generator class.
Useful to get the names of the generators from the python.

  new file:   pytriqs/random/*
  new file:   doc/reference/python/random/*
2013-09-16 12:20:06 +02:00

50 lines
1.4 KiB
Cython

from dcomplex cimport *
from libcpp.string cimport string
# The c++ random generator class
# We wrap two members only
cdef extern from "triqs/mc_tools/random_generator.hpp":
cdef cppclass random_generator "triqs::mc_tools::random_generator":
random_generator(string, long) except +
double operator() () except +
int operator() (int) except +
# This is the wrapping of the static member random_generator_names
# It is a bit of a hack but there is no notion of ststic members in cython
cdef extern from "triqs/mc_tools/random_generator.hpp" namespace "triqs::mc_tools::random_generator":
string random_generator_names(string) except +
# The python RandomGenerator class
cdef class RandomGenerator:
cdef random_generator * _c
def __init__(self, name, seed):
"""This is a random number generator class based on boost.
:param name: (string) Name of the random number generator
:param seed: (int) Random number seed
"""
self._c = new random_generator(name, seed)
def __dealloc__(self):
del self._c
def rand(self):
"""Generate a float random number in [0,1["""
return self._c[0]()
def int_rand(self, N):
"""Generate an integer random number in [0,N-1]"""
return self._c[0](N)
# The list of generator names accessed through the static member
def available_generator_names():
"""Get a list of available random generator names"""
return random_generator_names(",").split(',')