diff --git a/doc/reference/python/contents.rst b/doc/reference/python/contents.rst index 4cb336e2..04bd1850 100644 --- a/doc/reference/python/contents.rst +++ b/doc/reference/python/contents.rst @@ -9,4 +9,5 @@ Python operators/operators lattice/lattice data_analysis/contents + random/random diff --git a/doc/reference/python/operators/operators.rst b/doc/reference/python/operators/operators.rst index 23f84ba5..f12bb3d3 100644 --- a/doc/reference/python/operators/operators.rst +++ b/doc/reference/python/operators/operators.rst @@ -1,5 +1,6 @@ -.. _operators: +.. module:: pytriqs.random +.. _operators: The Operator class =================== diff --git a/doc/reference/python/random/random.py b/doc/reference/python/random/random.py new file mode 100644 index 00000000..7e76182e --- /dev/null +++ b/doc/reference/python/random/random.py @@ -0,0 +1,7 @@ +from pytriqs.random import * +from pytriqs.plot.mpl_interface import * + +r = RandomGenerator("lagged_fibonacci607", 237489) +l = [] +for i in range(10000): l += [r.rand(),] +plt.hist(l, 30, normed=True); diff --git a/doc/reference/python/random/random.rst b/doc/reference/python/random/random.rst new file mode 100644 index 00000000..c4139f03 --- /dev/null +++ b/doc/reference/python/random/random.rst @@ -0,0 +1,48 @@ +.. index:: Random number generator + +.. module:: pytriqs.random + +.. _random_generator: + +Random generator class +====================== + +TRIQS has a simple random number generator class called ``RandomGenerator``. It is based on boost +just like the C++ random generator provided by TRIQS. + +Usage +----- + +The generator is constructed from a name and a seed:: + + from pytriqs.random import * + r = RandomGenerator("mt19937", 237849) + +A list of possible random generator names is obtained with:: + + print available_generator_names() + +Then you can either generate float number on the interval :math:`[0,1[` using +the ``rand()`` method or integer numbers in the inverval :math:`[0,N-1]` using +``int_rand(N)``:: + + print r.rand() + print r.int_rand(10) + +Example +------- + +Here's a simple example showing how to use the generator. + +.. plot:: reference/python/random/random.py + :include-source: + :scale: 70 + + +Complete reference +------------------ + +.. autoclass:: pytriqs.random.RandomGenerator + :members: + +.. autofunction:: pytriqs.random.available_generator_names diff --git a/pytriqs/random/CMakeLists.txt b/pytriqs/random/CMakeLists.txt new file mode 100644 index 00000000..35ac5dc2 --- /dev/null +++ b/pytriqs/random/CMakeLists.txt @@ -0,0 +1,8 @@ +SET(PYTHON_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py +) + +install (FILES ${PYTHON_SOURCES} DESTINATION ${TRIQS_PYTHON_LIB_DEST}/random) + +# the c++ code +cython_module(random random_generator random) diff --git a/pytriqs/random/__init__.py b/pytriqs/random/__init__.py new file mode 100644 index 00000000..ce5e631f --- /dev/null +++ b/pytriqs/random/__init__.py @@ -0,0 +1,5 @@ + +from random_generator import RandomGenerator, available_generator_names + +__all__ = ['RandomGenerator','available_generator_names'] + diff --git a/pytriqs/random/random_generator.pyx b/pytriqs/random/random_generator.pyx new file mode 100644 index 00000000..7d0b7878 --- /dev/null +++ b/pytriqs/random/random_generator.pyx @@ -0,0 +1,49 @@ +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(',') +