Multithreading in c++ as a random number generator

Viewed 113

I was reading up on multithreading in c++ and found out that if you'd run code like

#include <isostream>
#include <thread>

void function1(){
    for(int i = 0; i<200; i++){
        std::cout << '+';
    }
}
void function2(){
    for(int i = 0; i<200; i++){
        std::cout << '-';
    }
}
int main() {
    std::thread worker1(function1);
    std::thread worker2(function2);
    
    system("pause>nul");
}

The plusses and minusses are in random order as this is dependent on what thread is finishing first, my question is then if this form of racing between threads could be a good random number generator and why (not).
Thanks in advance

1 Answers

No. You cannot do it, if you really need random numbers. Even as a seed you may not uses it. There is not enough entropy.

Speedwise it would be also totally insufficient.

I remember that when I was working in the gaming industry, some really good mathematiciens worked on random numbers for a long time and official governnment authorities certified the random number generators after weeks of test.

The authors of the C++ <random> library made the same efforts. To come up with better and faster algorithms may be extremely difficulty.

Additionally, maybe the behaviour with multithreading is even deterministic. For cooperative and preemtive (with priorities) multitasking this is most probably true.

But nice idea . . .

Related