RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Getting started
Back to Installing RandomLib. Forward to Code organization. Up to Contents.

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

The following gives a quick idea of the capabilities of the library. Note that the classes are all defined in the RandomLib namespace.

unsigned u; unsigned long long l;
float f; double d; long double e; bool b;
RandomLib::Random r; // create random number object
r.Reseed(); // seed with a "unique" seed
std::cout << "Using " << r.Name() << "\n"
<< "with seed " << r.SeedString() << "\n";
f = r.FixedU<float>(); // result in (0,1] with 24 bits of randomness
d = r.Fixed(); // result in [0,1) with 53 bits of randomness
e = r.FixedN<long double>(); // result in [0,1] with 64 bits of randomness
d = r.FloatN(); // a random in [0,1] rounded to the nearest double
u = r.Integer(); // a random unsigned in [0,2^32)
l = r.Integer<unsigned long long>(); // a random unsigned long long
u = r.Integer<16>(); // a random unsigned in [0,2^16)
u = r.Integer(52U); // a random unsigned in [0,52)
u = r(52); // the same, enables passing r to std::random_shuffle
u = r.IntegerC(1,6); // a random integer in [1,6]
b = r.Boolean(); // true with prob 1/2
b = r.Prob(4, 7); // true with prob 4/7
b = r.Prob(0.34); // true with prob 0.34
d = g(r); // sample from normal distribution
d = x(r); // sample from exponential distribution

Here is a complete example, RandomExample.cpp.

/**
* \file RandomExample.cpp
* \brief Simple examples of use of %RandomLib
*
* Compile/link with, e.g.,\n
* g++ -I../include -O2 -funroll-loops
* -o RandomExample RandomExample.cpp ../src/Random.cpp\n
* ./RandomExample
*
* This provides a simple illustration of some of the capabilities of
* %RandomLib.
*
* Copyright (c) Charles Karney (2006-2011) <charles@karney.com> and licensed
* under the MIT/X11 License. For more information, see
* http://randomlib.sourceforge.net/
**********************************************************************/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main(int, char**) {
RandomLib::Random r; // Create r
r.Reseed(); // and give it a unique seed
std::cout << "Using " << r.Name() << "\n"
<< "with seed " << r.SeedString() << "\n";
{
std::cout << "Estimate pi = ";
size_t in = 0, num = 10000;
for (size_t i = 0; i < num; ++i) {
// x, y are in the interval (-1/2,1/2)
double x = r.FixedS(), y = r.FixedS();
if (x * x + y * y < 0.25) ++in; // Inside the circle
}
std::cout << (4.0 * in) / num << "\n";
}
{
std::cout << "Tossing a coin 20 times: ";
for (size_t i = 0; i < 20; ++i) std::cout << (r.Boolean() ? "H" : "T");
std::cout << "\n";
}
std::cout << "Generate 20 random bits: " << r.Bits<20>() << "\n";
{
std::cout << "Throwing a pair of dice 15 times:";
for (size_t i = 0; i < 15; ++i)
std::cout << " " << r.IntegerC(1,6) + r.IntegerC(1,6);
std::cout << "\n";
}
{
// Weights for throwing a pair of dice
unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
// Initialize selection
RandomLib::RandomSelect<unsigned> sel(w, w + sizeof(w)/sizeof(unsigned));
std::cout << "A different way of throwing dice:";
for (size_t i = 0; i < 15; ++i) std::cout << " " << sel(r);
std::cout << "\n";
}
{
std::cout << "Draw balls from urn containing 5 red and 5 white balls: ";
int t = 10, w = 5;
while (t) std::cout << (r.Prob(w, t--) ? w--, "W" : "R");
std::cout << "\n";
}
{
std::cout << "Shuffling the letters a..z: ";
std::string digits = "abcdefghijklmnopqrstuvwxyz";
std::random_shuffle(digits.begin(), digits.end(), r);
std::cout << digits << "\n";
}
{
std::cout << "Estimate mean and variance of normal distribution: ";
double m = 0, s = 0;
int k = 0;
while (k < 10000) {
double x = n(r), m1 = m + (x - m)/++k;
s += (x - m) * (x - m1); m = m1;
}
std::cout << m << ", " << s/(k - 1) << "\n";
}
{
typedef float real;
enum { prec = 4 };
std::cout << "Some low precision reals (1/" << (1<<prec) << "):";
for (size_t i = 0; i < 5; ++i) std::cout << " " << r.Fixed<real, prec>();
std::cout << "\n";
}
std::cout << "Used " << r.Count() << " random numbers\n";
try {
// This throws an error if there's a problem
RandomLib::MRandom32::SelfTest();
std::cout << "Self test of " << RandomLib::MRandom32::Name()
<< " passed\n";
RandomLib::MRandom64::SelfTest();
std::cout << "Self test of " << RandomLib::MRandom64::Name()
<< " passed\n";
RandomLib::SRandom32::SelfTest();
std::cout << "Self test of " << RandomLib::SRandom32::Name()
<< " passed\n";
RandomLib::SRandom64::SelfTest();
std::cout << "Self test of " << RandomLib::SRandom64::Name()
<< " passed\n";
}
catch (std::out_of_range& e) {
std::cerr << "Self test FAILED: " << e.what() << "\n";
return 1;
}
return 0;
}

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

Back to Installing RandomLib. Forward to Code organization. Up to Contents.