Re: where are the implementations for <random>?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Second success: Uniform integer distribution from 1,...,n:

  typedef std::mt19937_64::result_type uint_t;
  // Returns element of 1,...,n with uniform probability:
  class Uniform {
    std::mt19937_64& g;
    const uint_t n;
    const uint_t scaling;
    const uint_t past;
  public :
    Uniform(std::mt19937_64& g, const uint_t n) : g(g), n(n), scaling(std::numeric_limits<uint_t>::max() / n), past(n * scaling) {}
    gen_uint_t operator ()() {
      gen_uint_t result;
      do result = g(); while (result >= past);
      return result/scaling + 1;
    }
  };

Now only missing seeding for std::mt19937_64.

This seems most complicated.
Apparently it is in
https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/include/bits/random.tcc?revision=246542&view=markup

Lines 346 - 389.

I would guess that this functionality is most specialised, and most likely to be different
for different compilers.

Really a pitty that this was not standardised.

Oliver

On Tue, Apr 11, 2017 at 02:02:42PM +0100, Oliver Kullmann wrote:
> thanks for the info.
> 
> My first, little, basically trivially success, is replacing the
> Bernoulli-distribution for the default case:
> 
>   inline bool bernoulli(std::mt19937_64& g) noexcept {
>     typedef std::mt19937_64::result_type uint_t;
>     constexpr uint_t half = std::numeric_limits<uint_t>::max() / 2;
>     return g() < half;
>   }
> 
> Checked that it reproduces the behaviour of gcc 4.7.4 and 5.4.
> 
> Replacing std::uniform_int_distribution (only for std::mt19937_64) seems harder,
> and yet I have problems understanding the code.
> 
> One could just generalise the above, and since only 32-bits intervals
> are used, that likely would suffice for a decent distribution.
> 
> However g++ seems to do something more advanced.
> 
> On Sun, Apr 09, 2017 at 05:31:54PM -0600, Martin Sebor wrote:
> > It's worth keeping in mind that copying libstdc++ code will have
> > licensing implications.  A different caveat is that conforming C++
> > programs can define explicit specializations of standard library
> > templates only for user-defined types.  I.e., it's not valid to
> > define one's own std::uniform_int_ditribution class template or
> > specialization of it on a fundamental type such as int.
> >
> 
> I just want to extract the mathematical content, and then write simple
> code for it, like above.
> 
> If somebody knows some underlying papers describing the algorithms for
> uniform_int_distribution and seeding std::mt19937_64, that would be great.
> 
> > Another option is to browse the sources online:
> > 
> > https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/include/bits/random.h?view=markup
> >
> 
> Thanks, I found that most useful.
> 
> Oliver
> 




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux