title: How to create a Matrix of random Doubles using Vectors?
body: <p>I'm having trouble creating a Matrix using Vectors. The rows and columns will all be random doubles and I am trying to fill it in.</p>
  
  <p>I have tried to initialize the size of the vector before hand, I believe it initializes correctly, however, when I try to push onto the rows and columns I get very odd output. Here is my Header File:</p>
  
  <pre><code>#ifndef MATRIX_NORM_HPP
  #define MATRIX_NORM_HPP
  
  #include &lt;vector&gt;
  
  class MatrixNorm
  {
  
  public:
      void initProgram();
      void printResults();
  
      double randNumber();
  
  private:
  
      std::vector&lt;std::vector&lt;double&gt;&gt; M;
      double mNorm1 = 0.0;
      double mNormInf = 0.0;
  
  };
  
  #endif
  </code></pre>
  
  <p>Here is my CPP File:</p>
  
  <pre><code>#include "matrixNorm.hpp"
  #include &lt;iostream&gt;
  #include &lt;random&gt;
  
  void initProgram()
  {
      double ranNum = 0.0;
      int size = 0;   
      std::cout &lt;&lt; "Please enter a size of an n by n Matrix: ";
      std::cin &gt;&gt; size;
  
      std::vector&lt;std::vector&lt;double&gt;&gt; temp(size, std::vector&lt;double&gt;(size));
  
  
      for(int i = 0; i &lt; size; ++i)
      {
          for(int j = 0; j &lt; size; ++j)
          {
              ranNum = randNumber();
              temp[i].push_back(ranNum);
              temp[j].push_back(ranNum);
          }
      }
  
      M = temp;
      printResults();     
  
  }
  
  
  void MatrixNorm::printResults()
  {
      for(auto &amp;&amp;e: M)
      {
          for(auto &amp;&amp; f: e)
          {
              std::cout &lt;&lt; f &lt;&lt; " ";
          }
          std::cout &lt;&lt; std::endl;
      }
  }
  
  double MatrixNorm::randNumber()
  {
      double ranDouble = 0.0;
      std::random_device rd;
      std::default_random_engine generator(rd());
      std::uniform_real_distribution&lt;double&gt; unif(-1000.0,1000.0);
  
      ranDouble = unif(generator);
  
      return ranDouble;
  }
  </code></pre>
  
  <p>The output I receive when I run the program from main.cpp is:</p>
  
  <pre><code>Please enter a size of an n by n Matrix: 3
  0 0 0 792.208 792.208 -361.248 -776.871 742.521 116.732 
  0 0 0 -361.248 742.521 411.965 411.965 909.313 -50.0048 
  0 0 0 -776.871 909.313 116.732 -50.0048 79.6189 79.6189 
  </code></pre>
  
  <p>As you can see, it seems to get the column size correctly, but it does not get the row size correctly, and if you look very closely. Some of the numbers are duplicates, I wish I knew how to format it more clearly but if you start at the top left you see <code>792.208 792.208</code> then go down a row and you see <code>411.965 411.965</code> and last it finishes off at <code>79.6189 79.6189</code> in the lower right.</p>
  
  <p>What am I doing wrong? How do I do this correctly? Any help would be appreciated.</p>