title: Randomly generate prime number
body: <p>I'm trying to randomly generate a prime number but it doesn't seem to be working and instead just throws out a normal number.</p>
  
  <p>I've tried playing around with the isPrime function, and changing where i generate the number</p>
  
  <pre class="lang-cpp prettyprint-override"><code>bool isPrime(int n) {
      int flag = 0;
      for (int i = 2; i &lt; n / 2; ++i) {
          if (n % i == 0) {
              flag = 1;
              break;
          }
          if (flag == 0) return true;
          else return false;
      }
  }
  void generatePublicKey() {
  
      int p = 0;
      int q = 0;
  
      while (!isPrime(p)) {
          srand(time(NULL));
          p = rand();
      }
  
      std::cout &lt;&lt; p;
  
  }
  </code></pre>