title: The correct ways to use rand() in selection structure
body: <p>Can anyone identify why the line  : if(code == rand() &amp;&amp; attempt != 3) not executed even I'm entered a correct code that generated by the rand().</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include&lt;time.h&gt;
   
  int main(void)
  {
  
      int code, attempt = 0;
      srand(time(0));
  
      for(int i = 0; i&lt;1; i++)
          printf(&quot;Your code is %d &quot;, rand());
      
      do{    
      printf(&quot;\nEnter code: &quot;);
      scanf(&quot;%d&quot;, &amp;code);
      attempt++;
      
      }while ( code != rand() &amp;&amp; attempt != 3);
      
      if(code == rand() &amp;&amp; attempt != 3)
    {
        printf(&quot;\n Correct code&quot;);
        printf(&quot;\n&quot;);
    }
    else
    {
     printf(&quot;Incorrect code\n&quot;);
    }
      return 0;
  }
  </code></pre>