summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/random/example
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/boost/libs/random/example
parentInitial commit. (diff)
downloadceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz
ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/random/example')
-rw-r--r--src/boost/libs/random/example/Jamfile.v213
-rw-r--r--src/boost/libs/random/example/die.cpp60
-rw-r--r--src/boost/libs/random/example/intersections.cpp78
-rw-r--r--src/boost/libs/random/example/password.cpp49
-rw-r--r--src/boost/libs/random/example/random_demo.cpp110
-rw-r--r--src/boost/libs/random/example/weighted_die.cpp55
6 files changed, 365 insertions, 0 deletions
diff --git a/src/boost/libs/random/example/Jamfile.v2 b/src/boost/libs/random/example/Jamfile.v2
new file mode 100644
index 000000000..c598e7864
--- /dev/null
+++ b/src/boost/libs/random/example/Jamfile.v2
@@ -0,0 +1,13 @@
+# Jamfile.v2
+#
+# Copyright (c) 2009
+# Steven Watanabe
+#
+# Distributed under the Boost Software License, Version 1.0. (See
+# accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+
+run die.cpp ;
+run weighted_die.cpp ;
+run password.cpp /boost//random ;
+run intersections.cpp /boost//random ;
diff --git a/src/boost/libs/random/example/die.cpp b/src/boost/libs/random/example/die.cpp
new file mode 100644
index 000000000..5626a2ed3
--- /dev/null
+++ b/src/boost/libs/random/example/die.cpp
@@ -0,0 +1,60 @@
+// die.cpp
+//
+// Copyright (c) 2009
+// Steven Watanabe
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//[die
+/*`
+ For the source of this example see
+ [@boost://libs/random/example/die.cpp die.cpp].
+ First we include the headers we need for __mt19937
+ and __uniform_int_distribution.
+*/
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
+
+/*`
+ We use __mt19937 with the default seed as a source of
+ randomness. The numbers produced will be the same
+ every time the program is run. One common method to
+ change this is to seed with the current time (`std::time(0)`
+ defined in ctime).
+*/
+boost::random::mt19937 gen;
+/*`
+ [note We are using a /global/ generator object here. This
+ is important because we don't want to create a new [prng
+ pseudo-random number generator] at every call]
+*/
+/*`
+ Now we can define a function that simulates an ordinary
+ six-sided die.
+*/
+int roll_die() {
+ /*<< __mt19937 produces integers in the range [0, 2[sup 32]-1].
+ However, we want numbers in the range [1, 6]. The distribution
+ __uniform_int_distribution performs this transformation.
+ [warning Contrary to common C++ usage __uniform_int_distribution
+ does not take a /half-open range/. Instead it takes a /closed range/.
+ Given the parameters 1 and 6, __uniform_int_distribution
+ can produce any of the values 1, 2, 3, 4, 5, or 6.]
+ >>*/
+ boost::random::uniform_int_distribution<> dist(1, 6);
+ /*<< A distribution is a function object. We generate a random
+ number by calling `dist` with the generator.
+ >>*/
+ return dist(gen);
+}
+//]
+
+#include <iostream>
+
+int main() {
+ for(int i = 0; i < 10; ++i) {
+ std::cout << roll_die() << std::endl;
+ }
+}
diff --git a/src/boost/libs/random/example/intersections.cpp b/src/boost/libs/random/example/intersections.cpp
new file mode 100644
index 000000000..fea40d6a4
--- /dev/null
+++ b/src/boost/libs/random/example/intersections.cpp
@@ -0,0 +1,78 @@
+// intersections.cpp
+//
+// Copyright (c) 2018
+// Justinas V. Daugmaudis
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//[intersections
+/*`
+ For the source of this example see
+ [@boost://libs/random/example/intersections.cpp intersections.cpp].
+
+ This example demonstrates generating quasi-randomly distributed chord
+ entry and exit points on an S[sup 2] sphere.
+
+ First we include the headers we need for __niederreiter_base2
+ and __uniform_01 distribution.
+ */
+
+#include <boost/random/niederreiter_base2.hpp>
+#include <boost/random/uniform_01.hpp>
+
+#include <boost/math/constants/constants.hpp>
+
+#include <boost/tuple/tuple.hpp>
+
+/*`
+ We use 4-dimensional __niederreiter_base2 as a source of randomness.
+ */
+boost::random::niederreiter_base2 gen(4);
+
+
+int main()
+{
+ typedef boost::tuple<double, double, double> point_t;
+
+ const std::size_t n_points = 100; // we will generate 100 points
+
+ std::vector<point_t> points;
+ points.reserve(n_points);
+
+ /*<< __niederreiter_base2 produces integers in the range [0, 2[sup 64]-1].
+ However, we want numbers in the range [0, 1). The distribution
+ __uniform_01 performs this transformation.
+ >>*/
+ boost::random::uniform_01<double> dist;
+
+ for (std::size_t i = 0; i != n_points; ++i)
+ {
+ /*`
+ Using formula from J. Rovira et al., "Point sampling with uniformly distributed lines", 2005
+ to compute uniformly distributed chord entry and exit points on the surface of a sphere.
+ */
+ double cos_theta = 1 - 2 * dist(gen);
+ double sin_theta = std::sqrt(1 - cos_theta * cos_theta);
+ double phi = boost::math::constants::two_pi<double>() * dist(gen);
+ double sin_phi = std::sin(phi), cos_phi = std::cos(phi);
+
+ point_t point_on_sphere(sin_theta*sin_phi, cos_theta, sin_theta*cos_phi);
+
+ /*`
+ Here we assume that our sphere is a unit sphere at origin. If your sphere was
+ different then now would be the time to scale and translate the `point_on_sphere`.
+ */
+
+ points.push_back(point_on_sphere);
+ }
+
+ /*`
+ Vector `points` now holds generated 3D points on a sphere.
+ */
+
+ return 0;
+}
+
+//]
diff --git a/src/boost/libs/random/example/password.cpp b/src/boost/libs/random/example/password.cpp
new file mode 100644
index 000000000..273b4c817
--- /dev/null
+++ b/src/boost/libs/random/example/password.cpp
@@ -0,0 +1,49 @@
+// password.cpp
+//
+// Copyright (c) 2010
+// Steven Watanabe
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//[password
+/*`
+ For the source of this example see
+ [@boost://libs/random/example/password.cpp password.cpp].
+
+ This example demonstrates generating a random 8 character
+ password.
+ */
+
+
+#include <boost/random/random_device.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
+#include <iostream>
+
+int main() {
+ /*<< We first define the characters that we're going
+ to allow. This is pretty much just the characters
+ on a standard keyboard.
+ >>*/
+ std::string chars(
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "1234567890"
+ "!@#$%^&*()"
+ "`~-_=+[{]}\\|;:'\",<.>/? ");
+ /*<< We use __random_device as a source of entropy, since we want
+ passwords that are not predictable.
+ >>*/
+ boost::random::random_device rng;
+ /*<< Finally we select 8 random characters from the
+ string and print them to cout.
+ >>*/
+ boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
+ for(int i = 0; i < 8; ++i) {
+ std::cout << chars[index_dist(rng)];
+ }
+ std::cout << std::endl;
+}
+
+//]
diff --git a/src/boost/libs/random/example/random_demo.cpp b/src/boost/libs/random/example/random_demo.cpp
new file mode 100644
index 000000000..593c757d2
--- /dev/null
+++ b/src/boost/libs/random/example/random_demo.cpp
@@ -0,0 +1,110 @@
+/* boost random_demo.cpp profane demo
+ *
+ * Copyright Jens Maurer 2000
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id$
+ *
+ * A short demo program how to use the random number library.
+ */
+
+#include <iostream>
+#include <fstream>
+#include <ctime> // std::time
+
+#include <boost/random/linear_congruential.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_real.hpp>
+#include <boost/random/variate_generator.hpp>
+#include <boost/generator_iterator.hpp>
+
+// This is a typedef for a random number generator.
+// Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
+typedef boost::minstd_rand base_generator_type;
+
+// This is a reproducible simulation experiment. See main().
+void experiment(base_generator_type & generator)
+{
+ // Define a uniform random number distribution of integer values between
+ // 1 and 6 inclusive.
+ typedef boost::uniform_int<> distribution_type;
+ typedef boost::variate_generator<base_generator_type&, distribution_type> gen_type;
+ gen_type die_gen(generator, distribution_type(1, 6));
+
+ // If you want to use an STL iterator interface, use iterator_adaptors.hpp.
+ boost::generator_iterator<gen_type> die(&die_gen);
+ for(int i = 0; i < 10; i++)
+ std::cout << *die++ << " ";
+ std::cout << '\n';
+}
+
+int main()
+{
+ // Define a random number generator and initialize it with a reproducible
+ // seed.
+ base_generator_type generator(42);
+
+ std::cout << "10 samples of a uniform distribution in [0..1):\n";
+
+ // Define a uniform random number distribution which produces "double"
+ // values between 0 and 1 (0 inclusive, 1 exclusive).
+ boost::uniform_real<> uni_dist(0,1);
+ boost::variate_generator<base_generator_type&, boost::uniform_real<> > uni(generator, uni_dist);
+
+ std::cout.setf(std::ios::fixed);
+ // You can now retrieve random numbers from that distribution by means
+ // of a STL Generator interface, i.e. calling the generator as a zero-
+ // argument function.
+ for(int i = 0; i < 10; i++)
+ std::cout << uni() << '\n';
+
+ /*
+ * Change seed to something else.
+ *
+ * Caveat: std::time(0) is not a very good truly-random seed. When
+ * called in rapid succession, it could return the same values, and
+ * thus the same random number sequences could ensue. If not the same
+ * values are returned, the values differ only slightly in the
+ * lowest bits. A linear congruential generator with a small factor
+ * wrapped in a uniform_smallint (see experiment) will produce the same
+ * values for the first few iterations. This is because uniform_smallint
+ * takes only the highest bits of the generator, and the generator itself
+ * needs a few iterations to spread the initial entropy from the lowest bits
+ * to the whole state.
+ */
+ generator.seed(static_cast<unsigned int>(std::time(0)));
+
+ std::cout << "\nexperiment: roll a die 10 times:\n";
+
+ // You can save a generator's state by copy construction.
+ base_generator_type saved_generator = generator;
+
+ // When calling other functions which take a generator or distribution
+ // as a parameter, make sure to always call by reference (or pointer).
+ // Calling by value invokes the copy constructor, which means that the
+ // sequence of random numbers at the caller is disconnected from the
+ // sequence at the callee.
+ experiment(generator);
+
+ std::cout << "redo the experiment to verify it:\n";
+ experiment(saved_generator);
+
+ // After that, both generators are equivalent
+ assert(generator == saved_generator);
+
+ // as a degenerate case, you can set min = max for uniform_int
+ boost::uniform_int<> degen_dist(4,4);
+ boost::variate_generator<base_generator_type&, boost::uniform_int<> > deg(generator, degen_dist);
+ std::cout << deg() << " " << deg() << " " << deg() << std::endl;
+
+ {
+ // You can save the generator state for future use. You can read the
+ // state back in at any later time using operator>>.
+ std::ofstream file("rng.saved", std::ofstream::trunc);
+ file << generator;
+ }
+
+ return 0;
+}
diff --git a/src/boost/libs/random/example/weighted_die.cpp b/src/boost/libs/random/example/weighted_die.cpp
new file mode 100644
index 000000000..8dd9c2ac7
--- /dev/null
+++ b/src/boost/libs/random/example/weighted_die.cpp
@@ -0,0 +1,55 @@
+// weighted_die.cpp
+//
+// Copyright (c) 2009
+// Steven Watanabe
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//[weighted_die
+/*`
+ For the source of this example see
+ [@boost://libs/random/example/weighted_die.cpp weighted_die.cpp].
+*/
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/discrete_distribution.hpp>
+
+boost::mt19937 gen;
+
+/*`
+ This time, instead of a fair die, the probability of
+ rolling a 1 is 50% (!). The other five faces are all
+ equally likely.
+
+ __discrete_distribution works nicely here by allowing
+ us to assign weights to each of the possible outcomes.
+
+ [tip If your compiler supports `std::initializer_list`,
+ you can initialize __discrete_distribution directly with
+ the weights.]
+*/
+double probabilities[] = {
+ 0.5, 0.1, 0.1, 0.1, 0.1, 0.1
+};
+boost::random::discrete_distribution<> dist(probabilities);
+
+/*`
+ Now define a function that simulates rolling this die.
+*/
+int roll_weighted_die() {
+ /*<< Add 1 to make sure that the result is in the range [1,6]
+ instead of [0,5].
+ >>*/
+ return dist(gen) + 1;
+}
+
+//]
+
+#include <iostream>
+
+int main() {
+ for(int i = 0; i < 10; ++i) {
+ std::cout << roll_weighted_die() << std::endl;
+ }
+}