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 "base.hh"
11#include "randomizer.hh"
12#include <boost/random/uniform_int.hpp>
13#include <boost/random/bernoulli_distribution.hpp>
14#include <boost/random/variate_generator.hpp>
15
16typedef boost::mt19937 & rng_t;
17
18bool randomizer::flip(size_t n)
19{
20 return bernoulli(1.0 / static_cast<double>(n));
21}
22
23size_t randomizer::uniform(size_t n)
24{
25 typedef boost::uniform_int<size_t> dist_t;
26 return boost::variate_generator<rng_t, dist_t>(rng, dist_t(0, n-1))();
27}
28
29bool randomizer::bernoulli(double p)
30{
31 typedef boost::bernoulli_distribution<double> dist_t;
32 return boost::variate_generator<rng_t, dist_t>(rng, dist_t(p))();
33}
34
35// Local Variables:
36// mode: C++
37// fill-column: 76
38// c-file-style: "gnu"
39// indent-tabs-mode: nil
40// End:
41// 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