bit::matrix — Random Fill

Factory method to construct a bit-matrix whose elements come from independent random draws from a Bernoulli distribution.

1static bit::matrix random(std::size_t r, std::size_t c, double prob_one);
2static bit::matrix random(std::size_t r, std::size_t c);
3static bit::matrix random(std::size_t n);
1
Returns an r x c bit-matrix where the probability that any element in the bit-matrix is 1 is prob_one.
2
Returns an r x c bit-matrix where the probability that any element in the bit-matrix is 1 is 0.5.
3
Returns an n x n square bit-matrix where the probability that an element in the bit-matrix is 1 is 0.5.

The probability that an element in the bit-matrix is 1 is prob_one. The default probability value is 0.5, so element values are determined by tossing a fair coin a total of r x c times.

These throw a std::invalid_argument exception if prob_one is not in the valid range \([0, 1]\).

Example

#include <bit/bit.h>
int main()
{
    auto m1 = bit::matrix<>::random(8);
    std::cout << m1 << std::endl;
}

Output (varies from run to run)

│1 0 1 1 1 1 1 0│
│1 1 0 0 1 1 1 0│
│1 1 0 0 0 0 1 0│
│1 0 1 0 0 1 1 0│
│1 1 0 1 1 0 1 1│
│0 0 0 1 0 0 1 0│
│1 0 0 0 1 1 0 0│
│1 1 0 0 1 1 1 0│

See Also

matrix::ones
matrix::zeros
matrix::identity
matrix::checker_board
matrix::shift
matrix::rotate

Back to top