|
RandomLib
1.10
|
Look at the code in the examples directory of the distribution for samples of code using RandomLib. In particular see RandomExample.cpp which illustrates several uses of random numbers and RandomCoverage.cpp while illustrates the calling sequence of many of the functions of RandomLib. Both these programs are compiled by default; see Installing RandomLib for details
In order to use RandomLib, you will need to
Building your code with cmake. In brief, the necessary steps are:
find_package (RandomLib 1.9 REQUIRED)
include_directories (${RandomLib_INCLUDE_DIRS})
add_definitions (${RandomLib_DEFINITIONS})
add_executable (program source1.cpp source2.cpp)
target_link_libraries (program ${RandomLib_LIBRARIES}) (The add_definitions line is only needed for Windows and can be omitted if you're using cmake version 2.8.11 or later.) mkdir BUILD
cd BUILD
cmake -G "Visual Studio 10" \
-D CMAKE_PREFIX_PATH=C:/pkg-vc10 \
-D CMAKE_PREFIX_PATH=C:/pkg-vc10/testrandom \
.. Note that you almost always want to configure and build your code somewhere other than the source directory (in this case, we use the BUILD subdirectory).cmake --build . --config Release --target ALL_BUILDYou might also want to install your package (using "make install" or build the "INSTALL" target with the command above).
The most import step is the find_package command. The cmake documentation describes the locations searched by find_package (the appropriate rule for RandomLib are those for "Config" mode lookups). In brief, the locations that are searched are (from least specific to most specific, i.e., in reverse order) are
C:/Program Files and /usr/local);CMAKE_PREFIX_PATH (illustrated above);RandomLib_DIR (which is the directory under which RandomLib is installed);RandomLib_DIR, which specifies the directory containing the configuration file randomlib-config.cmake (for debugging this may be the top-level build directory, as opposed to installation directory, for RandomLib).Typically, specifying nothing or CMAKE_PREFIX_PATH suffices. However the two RandomLib_DIR variables allow for a specific version to be chosen. On Windows systems (with Visual Studio), find_package will only find versions of RandomLib built with the right version of the compiler. (If you used a non-cmake method of installing RandomLib, you can try copying cmake/FindRandomLib.cmake to somewhere in your CMAKE_MODULE_PATH in order for find_package to work. However, this method has not been thoroughly tested.)
If RandomLib is found, then the following cmake variables are set:
RandomLib_FOUND = 1RandomLib_VERSION = 1.10RandomLib_INCLUDE_DIRSRandomLib_LIBRARIES = one of the following two:RandomLib_SHARED_LIBRARIES = RandomLibRandomLib_STATIC_LIBRARIES = RandomLib_STATICRandomLib_DEFINITIONS = one of the following two:RandomLib_SHARED_DEFINITIONS = -DRANDOMLIB_SHARED_LIB=1RandomLib_STATIC_DEFINITIONS = -DRANDOMLIB_SHARED_LIB=0RandomLib_LIBRARY_DIRSRandomLib_BINARY_DIRSEither of RandomLib_SHARED_LIBRARIES or RandomLib_STATIC_LIBRARIES may be empty, if that version of the library is unavailable. If you require a specific version, SHARED or STATIC, of the library, add a COMPONENTS clause to find_package, e.g.,
find_package (RandomLib 1.9 REQUIRED COMPONENTS SHARED)
causes only packages which include the shared library to be found. If the package includes both versions of the library, then RandomLib_LIBRARIES and RandomLib_DEFINITIONS are set to the shared versions, unless you include
set (RandomLib_USE_STATIC_LIBS ON)
before the find_package command. You can check whether RandomLib_LIBRARIES refers to the shared or static library with
get_target_property(_LIBTYPE ${RandomLib_LIBRARIES} TYPE) which results in _LIBTYPE being set to SHARED_LIBRARY or STATIC_LIBRARY. On Windows, cmake takes care of linking to the release or debug version of the library as appropriate. (This assumes that the Release and Debug versions of the libraries were built and installed. This is true for the Windows binary installer for RandomLib version 1.9 and later.)
g++ -c -g -O3 -funroll-loops -I/usr/local/include testprogram.cppWith Visual Studio, specify the include directory in the IDE via, e.g.,
C/C++ -> General -> Additional Include Directories = C:\pkg-vc10\RandomLib\include
RANDOMLIB_SHARED_LIB=1 (or 0), e.g., C/C++ -> Preprocessor -> Preprocessor Definitions = RANDOMLIB_SHARED_LIB=1This is only needed for Windows systems. (If you configure your package with cmake, this definition is added automatically.)
g++ -g -o testprogram testprogram.o -L/usr/local/lib -lRandomWith Visual Studio, you supply this information in the IDE via, e.g.,
Linker -> Input -> Additional Dependencies = Random-i.lib (for shared library)
Linker -> Input -> Additional Dependencies = Random.lib (for static library)
Linker -> General -> Additional Library Directories = C:\pkg-vc10\RandomLib\lib Note that the library name is Random and not RandomLib. For the Debug version of your program add "_d" to the library, e.g., Random_d-i.lib or Random_d.lib. g++ -g -o testprogram testprogram.o -Wl,-rpath=/usr/local/lib \
-L/usr/local/lib -lRandom (There are two other ways to specify the location of shared libraries at runtime: (1) define the environment variable LD_LIBRARY_PATH to be a colon-separated list of directories to search; (2) as root, specify /usr/local/lib as a directory searched by ldconfig(8).) On Windows, you need to ensure that Random.dll or Random_d.dll is in the same directory as your executable or else include the directory containing the dll in your PATH.The following gives a quick idea of the capabilities of the library. Note that the classes are all defined in the RandomLib namespace.
Here is a complete example, RandomExample.cpp.
Typical output from this program is:
Using RandomEngine<SFMT19937<Random_u32>,MixerSFMT> with seed [273392,1301667572,964,562213576,2011] Estimate pi = 3.1316 Tossing a coin 20 times: HHTHHHHTHTTHTTTHHTTT Generate 20 random bits: 10110101000101001001 Throwing a pair of dice 15 times: 3 4 6 5 3 10 8 6 7 7 3 6 12 6 9 A different way of throwing dice: 4 8 10 6 4 9 5 2 5 7 7 6 7 6 3 Draw balls from urn containing 5 red and 5 white balls: RWWWWRWRRR Shuffling the letters a..z: ibsymngwpcakhujqvdlrtexzfo Estimate mean and variance of normal distribution: -0.00648971, 0.981155 Some low precision reals (1/16): 0.6875 0 0.5625 0.125 0 Used 94637 random numbers Self test of RandomEngine<MT19937<Random_u32>,MixerSFMT> passed Self test of RandomEngine<MT19937<Random_u64>,MixerSFMT> passed Self test of RandomEngine<SFMT19937<Random_u32>,MixerSFMT> passed Self test of RandomEngine<SFMT19937<Random_u64>,MixerSFMT> passed
Here is a complete CMakeList.txt files you can use to build this test code using the installed library:
project (RandomExample)
cmake_minimum_required (VERSION 2.8.4)
find_package (RandomLib 1.9 REQUIRED)
if (NOT MSVC)
set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif ()
include_directories (${RandomLib_INCLUDE_DIRS})
add_definitions (${RandomLib_DEFINITIONS})
add_executable (${PROJECT_NAME} RandomExample.cpp)
target_link_libraries (${PROJECT_NAME} ${RandomLib_LIBRARIES})
if (MSVC)
get_target_property (_LIBTYPE ${RandomLib_LIBRARIES} TYPE)
if (_LIBTYPE STREQUAL "SHARED_LIBRARY")
# On Windows systems, copy the shared library to build directory
add_custom_command (TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy $<TARGET_FILE:${RandomLib_LIBRARIES}> ${CMAKE_CFG_INTDIR}
COMMENT "Copying shared library for RandomLib")
endif ()
endif () Certain macros can change the behavior of RandomLib. Define these before the inclusion of RandomLib/Random.hpp in your code. The macros are
#define HAVE_BOOST_SERIALIZATION 1#define RANDOMLIB_DEFAULT_GENERATOR name MRandomGenerator32 MRandomGenerator64 SRandomGenerator32 (default if unset) SRandomGenerator64The SRandomGeneratorNN generators are recommended. See Selection of default generator.
1.8.6