monotone

monotone Mtn Source Tree

Root/randomizer.cc

1// Copyright (C) 2006 Graydon Hoare <graydon@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "randomizer.hh"
11#include <boost/random.hpp>
12
13namespace randomizer
14{
15 static boost::mt19937 *rng = NULL;
16
17 boost::mt19937 &get_rng()
18 {
19 if (!rng)
20 rng = new boost::mt19937;
21 return *rng;
22 }
23
24 bool flip(size_t n)
25 {
26 return bernoulli(1.0 / static_cast<double>(n));
27 }
28
29 size_t uniform(size_t n)
30 {
31 return boost::random_number_generator<boost::mt19937>(get_rng())(n);
32 }
33
34 bool bernoulli(double p)
35 {
36 typedef boost::mt19937& rng_t;
37 typedef boost::bernoulli_distribution<double> dist_t;
38 return boost::variate_generator<rng_t, dist_t>(get_rng(), dist_t(p))();
39 }
40
41}
42
43// Local Variables:
44// mode: C++
45// fill-column: 76
46// c-file-style: "gnu"
47// indent-tabs-mode: nil
48// End:
49// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status