问题
I am attempting to make a fast password generator using multithreading with OpenMP integrated into Visual Studio 2010.
Let's say I have this basic string generator that randomly pulls Chars from a string.
srand(time(0));
for (i = 0; i < length; ++i)
{
s=pwArr[rand()%(pwArr.size()-1)];
pw+=s;
}
return pw;
Now, the basic idea is to enable multithreading with OpenMP to enable really fast random char lookup, like so:
srand(time(0));
#pragma omp parallel for
for (i = 0; i < length; ++i)
{
s=pwArr[rand()%(pwArr.size()-1)];
pw+=s;
}
return pw;
However, this only makes each thread do their own separate implementation of the password generator at the same time, and I end up getting repeats in my string.
An example output would be
ndxP1k1kP1k
Furthermore, this has terrible consequences with larger strings: Debug Assertation Failed error.
Am I just using the wrong tool for the wrong job?
回答1:
To answer your question, and turn @CrazyCasta's comment into an answer:
Yes, you are using the wrong tool for the wrong job
rand
isn't parallelisable as you want it to be, and since generating a password of any reasonable length is very fast already it's difficult to see why you would bother.
However, the topic of parallel pseudo-random number generators is an interesting one and there is a lot of published work on them, even some questions and good answers here on SO. I suggest you direct your attention to some more learning and return to your programming when you know some more about them.
来源:https://stackoverflow.com/questions/13889065/openmp-multithreading-on-a-random-password-generator