Boost logo

Boost :

Subject: Re: [boost] Is there any interest in getting pseudo random number generation using preprocessor at compile time into the Preprocessor library?
From: MURAKAMI Genya (bolero.murakami+cpp_at_[hidden])
Date: 2012-08-20 12:52:44


Hi, Gordon.

> On Aug 17, 2012, at 5:35 AM, "Alexander Stoyan" <alexander.stoyan_at_[hidden]> wrote:
> I have a need for pseudorandom numbers at compile time to test compile-time graph algorithms; however I guess a template metaprogram rather than preprocessor metaprogram is sufficient for what I'm trying to do.

If you need the compile-time pseudo-random numbers (rather than
preprocess-time), constexpr is good for you want to do.

Sprout.Random library provides a constexpr pseudo-random numbers and
probability distributions.
https://github.com/bolero-MURAKAMI/Sprout/tree/master/sprout/random

For example:

#include <sprout/random.hpp>
using namespace sprout;
constexpr auto prng = minstd_rand0(SPROUT_UNIQUE_SEED);
constexpr auto dist = uniform_smallint<int>(1, 6);

constexpr auto n0 = dist(prng); // returns pair of generated number
and next generator.
constexpr auto n1 = n0(); // call new generator.
constexpr auto n2 = n1(); // call new generator.
/*...*/
constexpr int v[] = { n0.result(), n1.result(), n2.result() /*, ...*/ };

You can also make a list of pseudo-random numbers in this way. :)

#include <sprout/array.hpp>
#include <sprout/algorithm.hpp>
constexpr auto v2 = sprout::copy( random::begin(prng, dist),
random::end(prng, dist), array<int, N>{{}} );

(I tested this on gcc4.7.1)

I'm sorry if my English is hard to understand.

Best regards.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk