vendredi 28 avril 2017

How to test std::bitset for randomity?

I create random bitsets with following code below. Now I wanna write some test and asking myself how to test the randomness of bitsets? Is there a good solution to this problem?

As bitsets can not be represented as numbers AFAIK, I don't know hot to apply some sort of test like chi-square or whatever.

Should I count the ocurrences of 1s and 0s like

 1001
+0101
+1110
+0010
 ----
 2222 = good as half of 4 (bitsets) equal 2

Are there other ways?

template <size_t N>
using bitset_t = std::bitset<N>;

and

template <size_t N>
struct random_bitset_t
{
  static_assert(N % 64 == 0, "random_bitset_t<N> only with N % 64 == 0");

  std::bitset<N> operator()() const;
};

template <size_t N>
std::bitset<N> random_bitset_t<N>::operator()() const
{
  static thread_local std::mt19937 g{std::random_device{}()};

  static thread_local std::uniform_int_distribution<uint64_t> d;

  std::bitset<N> bitset;

  for (int i=0,j=N/64; i<j; ++i)
  {
    bitset <<= 64;

    bitset |= std::bitset<N>(d(g));
  }
  return bitset;
}

and

template <size_t N>
using bitsets_t = std::vector<bitset_t<N>>;

and

template <size_t N>
struct random_bitsets_t
{
  random_bitsets_t(size_t);

  bitsets_t<N> operator()() const;

  size_t size;
};

template <size_t N>
random_bitsets_t<N>::random_bitsets_t(size_t size)
: size(size)
{}

template <size_t N>
bitsets_t<N> random_bitsets_t<N>::operator()() const
{
  bitsets_t<N> bitsets;

  bitsets.reserve(size);

  std::generate_n(std::back_inserter(bitsets),size,random_bitset_t<N>());

  return bitsets;
}

Aucun commentaire:

Enregistrer un commentaire