title: Random number generator sometimes fails to start in Eclipse
body: <p>I have written some code that produces an object which returns a unique random number when queried. However, every once in about 6 or 7 runs the program fails to start, and there seems to be no apparent reason for this. Also, my memory seems to drop over time (using a vector in the class). Any ideas?   </p>
  
  <pre><code> /*
   * Urn.h
   *
   *  Created on: Jan 17, 2014
   *      Author: edwinrietmeijer
   */
  
  #ifndef URN_H_
  #define URN_H_
  
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  class Urn {
  private:
      int highRand;
      int loRand;
      int numsTotal;
      int numsLeft;
      std::vector&lt;int&gt;numsAvailable;
  public:
      Urn();
      int getRand();
      int getSize();
      void reset();
      virtual ~Urn();
  };
  
  #endif /* URN_H_ */
  </code></pre>
  
  <hr>
  
  <pre><code>/*
   * Urn.cpp
   *
   *  Created on: Jan 17, 2014
   *      Author: edwinrietmeijer
   */
  
  #include "Urn.h"
  
  using namespace std;
  
  Urn::Urn() : highRand( 9 ), loRand( 0 ) {
  
      vector&lt;int&gt;::iterator pos;
      int newRandom;
      numsTotal = highRand - loRand;
      bool newRandomExists = false;
  
      while ( numsAvailable.size() &lt; numsTotal + 1 )
      {
          do  {
              newRandomExists = false;
              newRandom = ( rand() % ( numsTotal + 1 ) ) + loRand;
              for ( pos = numsAvailable.begin(); pos != numsAvailable.end(); pos++ ) {
              //  cout &lt;&lt; *pos &lt;&lt; " ";
                  if ( *pos == newRandom ) {
                      newRandomExists = true;
                  }
              }
          //  cout &lt;&lt; "  Random -&gt; " &lt;&lt; newRandom &lt;&lt; " " &lt;&lt; newRandomExists &lt;&lt;  endl;
          } while ( newRandomExists == true );
          newRandomExists = false;
          numsAvailable.push_back( newRandom );
      }
  
      cout &lt;&lt; endl;
      // List the list
      for (pos = numsAvailable.begin(); pos != numsAvailable.end(); pos++ ) {
  //      cout &lt;&lt; *pos &lt;&lt; " ";
      }
  //  cout &lt;&lt; endl;
      // TODO Auto-generated constructor stub
  }
  
  int Urn::getRand() {
      vector&lt;int&gt;::iterator pos = numsAvailable.begin();
      int numToReturn = numsAvailable.back( );
      numsAvailable.erase( pos  );
      return numToReturn;
  }
  
  int Urn::getSize() {
      return numsAvailable.size();
  }
  
  void Urn::reset() {
  
  }
  
  
  Urn::~Urn() {
      // TODO Auto-generated destructor stub
  }
  </code></pre>
  
  <hr>
  
  <pre><code>//============================================================================
  // Name        : UniqueRand.cpp
  // Author      : Edwin Rietmeijer
  // Version     :
  // Copyright   : This code is owned by Edwin Rietmeijer as of 2014
  // Description : Hello World in C++, Ansi-style
  //============================================================================
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "Urn.h"
  
  using namespace std;
  
  int main() {
      cout &lt;&lt; "Seeding..." &lt;&lt; endl;
  
      srand( time( NULL ) );
  
      cout &lt;&lt; "Getting object..." &lt;&lt; endl;
  
      Urn * urnObject = new Urn;
  
      cout &lt;&lt; urnObject -&gt; getRand();
  
      return 0;
  }
  </code></pre>