<commit_before>/**
 *  \file
 *  \remark This file is part of VITA.
 *
 *  \copyright Copyright (C) 2011-2014 <NAME>.
 *
 *  \license
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this file,
 *  You can obtain one at http://mozilla.org/MPL/2.0/
 */

#if !defined(VITA_RANDOM_H)
#define      VITA_RANDOM_H

#include <cstdlib>
#include <random>
#include <set>

#include "kernel/vita.h"

namespace vita
{
  class random
  {
  public:
    template<class T> static T between(T, T);
    template<class T> static T sup(T);

    template<class C> static const typename C::value_type &element(const C &);
    template<class C> static typename C::value_type &element(C &);

    static unsigned ring(unsigned, unsigned, unsigned);

    static bool boolean(double = 0.5);

    static void seed(unsigned);
    void randomize();

    // The Mersenne Twister engine produces integers with a good
    // uniform distribution.
    // std::mt19937 and std::mt19937_64 are similar. There aren't
    // memory consumption differences and speed is almost equal.
    // Warning: checking for sizeof(int) <= 4 is perhaps not a very
    // portable way since on a 64-bit Windows machine, GCC (MinGW)
    // x64 compiler gives sizeof(int) = sizeof(long) = 4. So we use
    // void *.
    // using engine_t = std::conditional<sizeof(void *) <= 4,
    //                                   std::mt19937,
    //                                   std::mt19937_64>::type;
    using engine_t = std::mt19937;

    static engine_t &engine();
  };

  ///
  /// \return a reference to a single engine shared whereever needed.
  ///
  /// This function grants access to a shared engine.
  ///
  /// \note
  /// \c engine() must be enhanced with appropriate synchronization if the
  /// engine is to be shared among multiple threads.
  ///
  inline random::engine_t &random::engine()
  {
    // We are using a *global* generator object here. This is important because
    // we don't want to create a new pseudo-random number generator at every
    // call.
    // The numbers produced will be the same every time the program is run.
    static engine_t e{28071973u};  // Magic!!!

    return e;
  }

  ///
  /// \param[in] min minimum random number.
  /// \param[in] sup upper bound.
  /// \return a random \c double in the [min;sup[ range.
  ///
  /// This is a specialization for double values of the \c random::between(T, T)
  /// template function. The main difference is that here we use
  /// \c uniform_real<> distribution instead of \c uniform_int<>.
  ///
  /// \see <http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf>
  /// for further details.
  ///
  template<>
  inline
  double random::between(double min, double sup)
  {
    assert(min < sup);

    static std::uniform_real_distribution<double> d(min, sup);

    return d(engine());
  }

  ///
  /// \param[in] min minimum random number.
  /// \param[in] sup upper bound.
  /// \return a random number in the [min;sup[ range.
  ///
  /// Picks up a random integer value uniformly distributed in the set of
  /// integers {min, min+1, ..., sup-1}.
  ///
  /// \note
  /// Contrary to boost usage this function does not take a closed range.
  /// Instead it takes a half-open range (C++ usage and same behaviour of the
  /// real number distribution).
  ///
  template<class T>
  T random::between(T min, T sup)
  {
    static_assert(std::is_integral<T>::value,
                  "random::between needs a template-parameter of integer type");

    assert(min < sup);

    std::uniform_int_distribution<T> d(min, sup - 1);

    return d(engine());
  }

  ///
  /// \param[in] sup upper bound.
  /// \return a random number in the [0;sup[ range.
  ///
  /// \note
  /// This is a shortcut for: \c between<T>(0, sup);
  ///
  template<class T>
  T random::sup(T sup)
  {
    return between<T>(0, sup);
  }

  ///
  /// \param[in] c a STL container.
  /// \return a random element of container \a c.
  ///
  template<class C>
  const typename C::value_type &random::element(const C &c)
  {
    const auto size(c.size());
    assert(size);

    return *std::next(c.begin(), between<decltype(size)>(0, size));
  }

  ///
  /// \param[in] c a STL container.
  /// \return a random element of container \a c.
  ///
  template<class C>
  typename C::value_type &random::element(C &c)
  {
    const auto size(c.size());
    assert(size);

    return *std::next(c.begin(), between<decltype(size)>(0, size));
  }

  ///
  /// \param[in] p a probability ([0;1] range).
  /// \return \c true \a p% times.
  ///
  /// bool values are produced according to the Bernoulli distribution.
  ///
  inline
  bool random::boolean(double p)
  {
    assert(0.0 <= p && p <= 1.0);

    static std::bernoulli_distribution d{};

    using parm_t = decltype(d)::param_type;
    return d(engine(), parm_t{p});

    //return random::between<double>(0, 1) < p;
  }
}  // namespace vita

#endif  // Include guard
<commit_msg>[FIX] Some sign-related warnings with clang++<commit_after>/**
 *  \file
 *  \remark This file is part of VITA.
 *
 *  \copyright Copyright (C) 2011-2014 EOS di <NAME>.
 *
 *  \license
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this file,
 *  You can obtain one at http://mozilla.org/MPL/2.0/
 */

#if !defined(VITA_RANDOM_H)
#define      VITA_RANDOM_H

#include <cstdlib>
#include <random>
#include <set>

#include "kernel/vita.h"

namespace vita
{
  class random
  {
  public:
    template<class T> static T between(T, T);
    template<class T> static T sup(T);

    template<class C> static const typename C::value_type &element(const C &);
    template<class C> static typename C::value_type &element(C &);

    static unsigned ring(unsigned, unsigned, unsigned);

    static bool boolean(double = 0.5);

    static void seed(unsigned);
    void randomize();

    // The Mersenne Twister engine produces integers with a good
    // uniform distribution.
    // std::mt19937 and std::mt19937_64 are similar. There aren't
    // memory consumption differences and speed is almost equal.
    // Warning: checking for sizeof(int) <= 4 is perhaps not a very
    // portable way since on a 64-bit Windows machine, GCC (MinGW)
    // x64 compiler gives sizeof(int) = sizeof(long) = 4. So we use
    // void *.
    // using engine_t = std::conditional<sizeof(void *) <= 4,
    //                                   std::mt19937,
    //                                   std::mt19937_64>::type;
    using engine_t = std::mt19937;

    static engine_t &engine();
  };

  ///
  /// \return a reference to a single engine shared whereever needed.
  ///
  /// This function grants access to a shared engine.
  ///
  /// \note
  /// \c engine() must be enhanced with appropriate synchronization if the
  /// engine is to be shared among multiple threads.
  ///
  inline random::engine_t &random::engine()
  {
    // We are using a *global* generator object here. This is important because
    // we don't want to create a new pseudo-random number generator at every
    // call.
    // The numbers produced will be the same every time the program is run.
    static engine_t e{28071973u};  // Magic!!!

    return e;
  }

  ///
  /// \param[in] min minimum random number.
  /// \param[in] sup upper bound.
  /// \return a random \c double in the [min;sup[ range.
  ///
  /// This is a specialization for double values of the \c random::between(T, T)
  /// template function. The main difference is that here we use
  /// \c uniform_real<> distribution instead of \c uniform_int<>.
  ///
  /// \see <http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf>
  /// for further details.
  ///
  template<>
  inline
  double random::between(double min, double sup)
  {
    assert(min < sup);

    static std::uniform_real_distribution<double> d(min, sup);

    return d(engine());
  }

  ///
  /// \param[in] min minimum random number.
  /// \param[in] sup upper bound.
  /// \return a random number in the [min;sup[ range.
  ///
  /// Picks up a random integer value uniformly distributed in the set of
  /// integers {min, min+1, ..., sup-1}.
  ///
  /// \note
  /// Contrary to boost usage this function does not take a closed range.
  /// Instead it takes a half-open range (C++ usage and same behaviour of the
  /// real number distribution).
  ///
  template<class T>
  T random::between(T min, T sup)
  {
    static_assert(std::is_integral<T>::value,
                  "random::between needs a template-parameter of integer type");

    assert(min < sup);

    std::uniform_int_distribution<T> d(min, sup - 1);

    return d(engine());
  }

  ///
  /// \param[in] sup upper bound.
  /// \return a random number in the [0;sup[ range.
  ///
  /// \note
  /// This is a shortcut for: \c between<T>(0, sup);
  ///
  template<class T>
  T random::sup(T sup)
  {
    return between<T>(0, sup);
  }

  ///
  /// \param[in] c a STL container.
  /// \return a random element of container \a c.
  ///
  template<class C>
  const typename C::value_type &random::element(const C &c)
  {
    const auto size(c.size());
    assert(size);

    return *std::next(c.begin(),
                      static_cast<typename C::difference_type>(
                        between<decltype(size)>(0, size)));
  }

  ///
  /// \param[in] c a STL container.
  /// \return a random element of container \a c.
  ///
  template<class C>
  typename C::value_type &random::element(C &c)
  {
    const auto size(c.size());
    assert(size);

    return *std::next(c.begin(),
                      static_cast<typename C::difference_type>(
                        between<decltype(size)>(0, size)));
  }

  ///
  /// \param[in] p a probability ([0;1] range).
  /// \return \c true \a p% times.
  ///
  /// bool values are produced according to the Bernoulli distribution.
  ///
  inline
  bool random::boolean(double p)
  {
    assert(0.0 <= p && p <= 1.0);

    static std::bernoulli_distribution d{};

    using parm_t = decltype(d)::param_type;
    return d(engine(), parm_t{p});

    //return random::between<double>(0, 1) < p;
  }
}  // namespace vita

#endif  // Include guard

{'fraction_non_alphanumeric': 0.14321877129782884, 'fraction_numerical': 0.016940901567520202, 'mean_word_length': 2.364559449721585, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1251605', 'n_tokens_mistral': 3417, 'n_tokens_neox': 3200, 'n_words': 1093}