随机密码生成器上的OpenMP多线程 [英] OpenMP Multithreading on a Random Password Generator

查看:72
本文介绍了随机密码生成器上的OpenMP多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用集成了Visual Studio 2010的OpenMP的多线程来制作快速密码生成器.

I am attempting to make a fast password generator using multithreading with OpenMP integrated into Visual Studio 2010.

比方说,我有一个基本的字符串生成器,它可以从字符串中随机抽取Chars.

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;

现在,基本思想是使用OpenMP启用多线程以启用真正快速的随机字符查找,如下所示:

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.

示例输出为

ndxP1k1kP1k

此外,使用较大的字符串会产生可怕的后果:调试断言失败错误.

Furthermore, this has terrible consequences with larger strings: Debug Assertation Failed error.

我只是在为错误的工作使用了错误的工具吗?

Am I just using the wrong tool for the wrong job?

推荐答案

要回答您的问题,并将@CrazyCasta的评论变成答案:

To answer your question, and turn @CrazyCasta's comment into an answer:

是的,您为错误的工作使用了错误的工具

rand 不能像您希望的那样并行化,并且由于生成任意合理长度的密码已经非常快了,因此很难知道您为什么要打扰.

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.

但是,并行伪随机数生成器这一主题很有趣,关于它们的工作很多,甚至在SO上也有一些问题和很好的答案.我建议您将注意力转移到更多的学习上,并在对它们有更多了解时返回到编程.

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.

这篇关于随机密码生成器上的OpenMP多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆