<ol>
<li>Be sure that each thread has its <strong>own private PRNG</strong> (using one shared PRNG by all threads would need locking and would be terribly slow due to waiting and cache contention).</li>
<li>Katzgraber has proposed a seeding of PRNGs based on process/thread numbers in Section 7.1 of <a href="http://arxiv.org/pdf/1005.4117v1.pdf" rel="nofollow">Random Numbers in Scientific Computing:
An Introduction</a>.</li>
</ol>

<p>It looks like:</p>

<pre><code>long seedgen(void) {
    long s, seed, pid; // pid from 0 to number of processes/threads - 1
    pid = ...; /* get processt/thread ID */
    s = time ( &amp;seconds ); /* get CPU seconds since 01/01/1970 */
    seed = abs(((s * 181) * ((pid - 83) * 359)) % 104729);
    return seed;
}
</code></pre>
Threads, how to seed random number generator independently?
<p>I asked a question here yesterday about threads and getting input to create the amount of threads the user specifies. I was able to figure it out with you guy's help. I am working on the same program except this time, I need help with getting each thread in my program to independently seed the random number generator. M apologies if this sounds simple. I'm still new to threads.</p>

<p>Basically what my program is doing is asking the user how many threads they want to create, and how many arrows they want to throw. Each arrow will generate 2 numbers each, from -1 to 1. This is my program so far. It's working code so you can just run it if you need to:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;ctime&gt;
#include &lt;cstdlib&gt;
#include &lt;thread&gt;

using namespace std;

void exec(int n, int randNumbers)
{
    int seed = 0;

    srand(seed);
    int random_number = rand() % 1 + -1;

    cout &lt;&lt; "Thread " &lt;&lt; n &lt;&lt; endl;
    cout &lt;&lt; "\n";

    while (randNumbers != 0)
    {
        srand(seed);
        cout &lt;&lt; random_number &lt;&lt; endl;
        seed++;
        cout &lt;&lt; "Seed: " &lt;&lt; seed &lt;&lt; endl;
        cout &lt;&lt; "\n";

        cout &lt;&lt; random_number &lt;&lt; endl;
        seed++;
        cout &lt;&lt; "Seed: " &lt;&lt; seed &lt;&lt; endl;
        cout &lt;&lt; "\n";
        randNumbers--;
    }
}


int main()
{
    int numThreads = 0; // Threads
    int maxRandom; // Arrows

    cout &lt;&lt; "This is a Monte Carlo simulation." &lt;&lt; endl;
    cout &lt;&lt; "Please enter the number of threads to run." &lt;&lt; endl;
    cout &lt;&lt; "Threads: ";

    cin &gt;&gt; numThreads;

    // create an array of threads
    thread* myThreads = new thread[numThreads];

    if ((numThreads &gt; 20) || (numThreads &lt; 1))
    {
        cout &lt;&lt; "Sorry. Something went wrong." &lt;&lt; endl;
        return 0;
    }

    system("CLS");
    cout &lt;&lt; "\n";
    cout &lt;&lt; "Enter the number of arrows you would like to throw: " &lt;&lt; endl;
    cout &lt;&lt; "Arrows: ";

    cin &gt;&gt; maxRandom; // Arrows

    system("CLS");
    for (int i = 0; i &lt; numThreads; i++)
    {
        // run random number generator for thread at [i]
        myThreads[i] = thread(exec, i, maxRandom);
    }

    for (int i = 0; i &lt; numThreads; i++)
    {
        myThreads[i].join();
    }

    cout &lt;&lt; "Done!" &lt;&lt; endl;
}
</code></pre>

<p>All threads are returning -1 regardless of if <code>int seed</code> goes up by 1. I've looked all over but I still can't seem to figure out why my threads aren't independently seeding the random number generator. Anyone know what is going on? I'm still new to threads. Any bit of help would be greatly appreciated. Thank you very much.</p>