title: what are some optimization tricks to make my code run faster
body: <p>i'm moving outside my confront zone and trying to make a random number distribution program while also making sure it is still somewhat uniform.
  here is my code</p>
  
  <p>this is the RandomDistribution.h file</p>
  
  <pre><code>#pragma once
  #include &lt;vector&gt;
  #include &lt;random&gt;
  #include &lt;iostream&gt;
  
  
  
  static float randy(float low, float high) {
      static  std::random_device rd;
      static  std::mt19937 random(rd());
        std::uniform_real_distribution&lt;float&gt; ran(low, high);
      return ran(random);
  }
  
  
  typedef std::vector&lt;float&gt; Vfloat;
  class RandomDistribution
  {
  public:
  
      RandomDistribution();
      RandomDistribution(float percent, float contents, int container);
      ~RandomDistribution();
      void setvariables(float percent, float contents, int container);
      Vfloat RunDistribution();
  private:
      float divider;
      float _percent;
       int jar_limit;
      float _contents;
      float _maxdistribution;
      Vfloat Jar;
      bool is0;
  };
  </code></pre>
  
  <p>this is my RandomDistribution.cpp</p>
  
  <pre><code>#include "RandomDistribution.h"
  
  RandomDistribution::RandomDistribution() {
  
  }
  RandomDistribution::RandomDistribution(float percent, float contents, int containers):_contents(contents),jar_limit(containers)
  {
      Jar.resize(containers);
      if (percent &lt; 0)
          _percent = 0;
  
      else {
          _percent = percent;
      }
      divider = jar_limit * percent;
      is0 = false;
  }
  
  
  RandomDistribution::~RandomDistribution()
  {
  }
  void RandomDistribution::setvariables(float percent, float contents, int container) {
      if (jar_limit != container)
          Jar.resize(container);
  
      _contents = contents;
      jar_limit = container;
      is0 = false;
  
  
      if (percent &lt; 0)
          _percent = 0;
  
      else {
          _percent = percent;
      }
      divider = jar_limit * percent;
  }
  
  
  Vfloat RandomDistribution::RunDistribution() {
  
      for (int i = 0; i &lt; jar_limit; i++) {
  
          if (!is0) {
              if (i + 1 &gt;= jar_limit || _contents &lt; 2) {
                  Jar[i] = _contents;
                  _contents -= Jar[i];
                  is0 = true;
              }
  
              if (!_percent &lt;= 0) {//making sure it does not get the hole container at once
                  _maxdistribution = (_contents / (divider)) * (i + 1);
              }
              else {
                  _maxdistribution = _contents;
              }
  
              Jar[i] = randy(0, _maxdistribution);
  
              if (Jar[i] &lt; 1) {
                  Jar[i] = 0;
                  continue;
              }
  
              _contents -= Jar[i];
          }
          else {
              Jar[0];
          }
          //mixing Jar so it is randomly spaced out instead all at the top
          int swapper = randy(0, i);
          float hold = Jar[i];
          Jar[i] = Jar[swapper];
          Jar[swapper] = hold;
  
      }
  
      return Jar;
  }
  </code></pre>
  
  <p>source code</p>
  
  <pre><code>int main(){
      RandomDistribution distribution[100];
      for (int i = 0; i &lt; 100; i++) {
           distribution[i] = {RandomDistribution(1.0f, 5000.0f, 2000) };
      }
  
  
      Vfloat k;
      k.resize(200);
  
      for (int i = 0; i &lt; 10; i++) {
          auto t3 = chrono::steady_clock::now();
  
          for (int b = 0; b &lt; 100; b++) {
  
              k = distribution[b].RunDistribution();
              distribution[b].setvariables(1.0f, 5000.0f, 2000);
  
          }
  
          auto t4 = chrono::steady_clock::now();
          auto time_span = chrono::duration_cast&lt;chrono::duration&lt;double&gt;&gt;(t4 - t3);
          cout &lt;&lt; time_span.count() &lt;&lt; " seconds\n";
  
      }
  }
  </code></pre>
  
  <p>what prints out is usually between 1 to 2 seconds for each cycle. i want to bring it down to a tenth of a second if possible cause this is gonna be only one step of the process to completion and i want to run it alot more then 100 times. what can i do to speed this up, any trick or something i'm just missing here.
  here is a sample of the time stamps</p>
  
  <p>4.71113 seconds</p>
  
  <p>1.35444 seconds</p>
  
  <p>1.45008 seconds</p>
  
  <p>1.74961 seconds</p>
  
  <p>2.59192 seconds</p>
  
  <p>2.76171 seconds</p>
  
  <p>1.90149 seconds</p>
  
  <p>2.2822 seconds</p>
  
  <p>2.36768 seconds</p>
  
  <p>2.61969 seconds</p>