Random Boolean Matrix in C++

Asked

Viewed 124 times

-1

good afternoon.

I need to develop a Boolean matrix (almost a graph) random in C++, but I confess I used a lot of python, so I’m having a hard time getting started.

But the focus of the question is, when you’re programming something in C++, and you need to create an array, you use some library or develop the code as needed?

Is there a good library to work with vectors?

  • Hello! Your question is very general, and I took the liberty of answering as I understood why the topic interests me. Try to better define your intentions and show relevant snippets of the code you are writing. Also, Existe alguma biblioteca boa para trabalhar com vetores? He’s so open, he can’t answer. Maybe some forum on the subject is better than Stack Overflow (in the American site, questions like this are usually closed).

  • I gave a downvote that I intend to reverse if you improve the issue, see this as a constructive criticism =p

1 answer

1


Your question is very generic and then opens in a matter of personal opinion.

How to generate a random boolean matrix:

using namespace std;
int const LARGURA = 10;
int const ALTURA  =  5;

mt19937 gen(666); // gera números aleatórios mersenne twister
uniform_int_distribution<> dis(0, 1); //distribuição linear entre 0 e 1

vector<vector<int>> M;
M.resize(ALTURA);
for(auto & linha : M)
{ 
    linha.resize(LARGURA);
    for(auto & valor: linha)
    {
        valor = dis(gen); //sorteia 
    }
}

generates the result:

1 0 1 0 1 0 1 1 1 0 
0 0 0 0 0 0 0 0 1 0 
0 1 1 1 0 0 1 0 0 0 
1 1 0 1 0 1 0 1 0 1 
0 0 1 1 0 0 0 1 1 0 

Check the example above in: https://ideone.com/HwwzI1

As to

when you are programming something in C++, and you need to create an array, you use some library or develop the code as needed?

Depends on the project. C++ does not have Matrices in the mathematical sense, with arithmetic operations defined, for this type of operation it is better to use an external library.

In the example above I represented a Matrix as a vector of vectors.

Is there a good library to work with vectors?

Yes. Arrays and vectors have many applications and I advise you to find a library for the area of interest. Experiment with some and try to familiarize yourself with the strong typing of C++, which makes the code much more verbose than Python. Good studies and good luck =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.