Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the random_device implementation #34

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/doc/html
/doc/reference.xml
/test/rng.saved
**/rng.saved
30 changes: 30 additions & 0 deletions Jamfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Boost.Random Library Jamfile
#
# Copyright (c) 2017 James E. King, III
#
# Use, modification, and distribution are subject to 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)

project libs/random
: requirements

<warnings>all

<toolset>clang:<cxxflags>-Wextra
<toolset>clang:<cxxflags>-ansi
# <toolset>clang:<cxxflags>-pedantic
<toolset>clang:<cxxflags>-Wno-c++11-long-long

<toolset>gcc:<cxxflags>-Wextra
<toolset>gcc:<cxxflags>-ansi
# <toolset>gcc:<cxxflags>-pedantic
<toolset>gcc:<cxxflags>-Wno-long-long
;

# pedantic mode disabled due to issue in multiprecision
# https://github.com/boostorg/multiprecision/issues/34

# please order by name to ease maintenance
build-project example ;
build-project test ;
18 changes: 0 additions & 18 deletions build/Jamfile.v2

This file was deleted.

103 changes: 0 additions & 103 deletions doc/nondet_random.qbk

This file was deleted.

30 changes: 24 additions & 6 deletions doc/random.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,32 @@ You should read the [concepts concepts documentation] for an introduction and th
definition of the basic concepts. For a quick start, it may be sufficient
to have a look at [@boost:/libs/random/example/random_demo.cpp random_demo.cpp].

For a very quick start, here's an example:
For a very quick start, here's an example for rolling one die:

``[classref boost::random::mt19937]`` rng; // produces randomness out of thin air
// see pseudo-random number generators
``[classref boost::random::random_device]`` entropy; // operating-system provided entropy generator
``[classref boost::random::uniform_int_distribution]<>`` six(1,6);
// distribution that maps to 1..6
// see random number distributions
int x = six(rng); // simulate rolling a die
// distribution that maps to 1..6
// see random number distributions
int x = six(entropy); // simulate rolling a die

If you are going to be rolling a large number of dies, you can initialize a
__PseudoRandomNumberGenerator once, then reuse it. This may be more optimal
than accessing the operating-system provided entropy generator directly many times - as
always you should test that the solution meets your performance needs:

``[classref boost::random::random_device]`` entropy; // operating-system provided entropy generator
``[classref boost::random::mt19937]`` prng; // produces randomness out of thin air
// see pseudo-random number generators
``[classref boost::random::uniform_int_distribution]<>`` six(1,6);
// distribution that maps to 1..6
// see random number distributions
prng.seed(entropy); // seed the PRNG
int total = 0;
const size_t rolls = 65536;
for (size_t roll = 0; roll < rolls; ++roll) {
total += six(prng); // simulate rolling a die
}
double avg = static_cast<double>(total) / static_cast<double>(rolls);

[endsect]

Expand Down
17 changes: 16 additions & 1 deletion example/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
# accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

project libs/random/example
: requirements

# boost.jam defines BOOST_ALL_NO_LIB for builds
# which cannot be undefined?
<toolset>msvc:<define>BOOST_RANDOM_FORCE_AUTO_LINK
<toolset>gcc-mingw:<linkflags>"-lbcrypt"

# boost::random needs this setting for a warning free build:
<toolset>msvc:<define>_SCL_SECURE_NO_WARNINGS

# link static for easier debugging - uncomment if you need to debug...
# <link>static
;

run die.cpp ;
run weighted_die.cpp ;
run password.cpp /boost//random ;
run password.cpp ;
2 changes: 1 addition & 1 deletion example/password.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
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
Expand Down
40 changes: 0 additions & 40 deletions include/boost/random/detail/auto_link.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion include/boost/random/detail/generator_seed_seq.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* boost random/mersenne_twister.hpp header file
/* boost random/detail/generator_seed_seq.hpp header file
*
* Copyright Jens Maurer 2000-2001
* Copyright Steven Watanabe 2010
Expand Down
32 changes: 32 additions & 0 deletions include/boost/random/detail/random_provider_arc4random.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright (c) 2017 James E. King III
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENCE_1_0.txt)
//
// "A Replacement Call for Random"
// https://man.openbsd.org/arc4random.3
//

#include <stdlib.h>

namespace boost {
namespace random {
namespace detail {

class random_provider
{
public:
//! Obtain entropy and place it into a memory location
//! \param[in] buf the location to write entropy
//! \param[in] siz the number of bytes to acquire
void get_random_bytes(void *buf, size_t siz)
{
arc4random_buf(buf, siz);
}
};

} // detail
} // random
} // boost
79 changes: 79 additions & 0 deletions include/boost/random/detail/random_provider_bcrypt.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// Copyright (c) 2017 James E. King III
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENCE_1_0.txt)
//
// BCrypt provider for entropy
//

#include <boost/core/ignore_unused.hpp>
#include <boost/random/entropy_error.hpp>
#include <boost/throw_exception.hpp>
#include <boost/winapi/bcrypt.hpp>
#include <boost/winapi/get_last_error.hpp>

#if defined(BOOST_RANDOM_FORCE_AUTO_LINK) || (!defined(BOOST_ALL_NO_LIB) && !defined(BOOST_RANDOM_NO_LIB))
# define BOOST_LIB_NAME "bcrypt"
# define BOOST_AUTO_LINK_NOMANGLE
# include <boost/config/auto_link.hpp>
# undef BOOST_AUTO_LINK_NOMANGLE
#endif

namespace boost {
namespace random {
namespace detail {

class random_provider
{
public:
random_provider()
: hProv_(NULL)
{
boost::winapi::NTSTATUS_ status =
boost::winapi::BCryptOpenAlgorithmProvider(
&hProv_,
boost::winapi::BCRYPT_RNG_ALGORITHM_,
NULL,
0);

if (status)
{
BOOST_THROW_EXCEPTION(entropy_error(status, "BCryptOpenAlgorithmProvider"));
}
}

~random_provider() BOOST_NOEXCEPT
{
if (hProv_)
{
ignore_unused(boost::winapi::BCryptCloseAlgorithmProvider(hProv_, 0));
}
}

//! Obtain entropy and place it into a memory location
//! \param[in] buf the location to write entropy
//! \param[in] siz the number of bytes to acquire
void get_random_bytes(void *buf, size_t siz)
{
boost::winapi::NTSTATUS_ status =
boost::winapi::BCryptGenRandom(
hProv_,
static_cast<boost::winapi::PUCHAR_>(buf),
siz,
0);

if (status)
{
BOOST_THROW_EXCEPTION(entropy_error(status, "BCryptGenRandom"));
}
}

private:
boost::winapi::BCRYPT_ALG_HANDLE_ hProv_;
};

} // detail
} // random
} // boost
Loading