What's the Right Way to use the rand() Function in C++?
<p>I'm doing a book exercise that says to write a program that generates psuedorandom numbers. I started off simple with.</p>

<pre><code>#include "std_lib_facilities.h"

int randint()
{
    int random = 0;
    random = rand();
    return random;
}

int main()
{
    char input = 0;
    cout &lt;&lt; "Press any character and enter to generate a random number." &lt;&lt; endl;
    while (cin &gt;&gt; input)
    cout &lt;&lt; randint() &lt;&lt; endl;
    keep_window_open();
}
</code></pre>

<p>I noticed that each time the program was run, there would be the same "random" output. So I looked into random number generators and decided to try seeding by including this first in randint().</p>

<pre><code>    srand(5355);
</code></pre>

<p>Which just generated the same number over and over (I feel stupid now for implementing it.)</p>

<p>So I thought I'd be clever and implement the seed like this.</p>

<pre><code>srand(rand());
</code></pre>

<p>This basically just did the same as the program did in the first place but outputted a different set of numbers (which makes sense since the first number generated by rand() is always 41.) </p>

<p>The only thing I could think of to make this more random is to:</p>

<ol>
<li>Have the user input a number and set that as the seed (which would be easy to implement, but this is a last resort)
OR</li>
<li>Somehow have the seed be set to the computer clock or some other constantly changing number. </li>
</ol>

<p>Am I in over my head and should I stop now? Is option 2 difficult to implement? Any other ideas?</p>

<p>Thanks in advance.</p>
