最快的实现在C#中一个真正的随机数发生器 [英] Fastest implementation of a true random number generator in C#

查看:1016
本文介绍了最快的实现在C#中一个真正的随机数发生器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关 Random.Next(),对于加密安全随机适用于创建一个随机密码号的MSDN暗示<一个href=\"http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx\">RNGCryptoServiceProvider类

I was reading about Random.Next() that for "cryptographically secure random number suitable for creating a random password" MSDN suggests RNGCryptoServiceProvider Class

什么速度刑罚?还有就是要获得真正的随机数一些最快的方法?

What the speed penality? There is some fastest way to get true random numbers?

编辑:
随着Random.Next()我得到一个新的随机数。并与...

byte[] randomNumber = new byte[1];
RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
Gen.GetBytes(randomNumber);
int rand = Convert.ToInt32(randomNumber[0]);

我得到一个加密安全随机数
我想知道,如果上述code为快相比Random.Next()如果有一些快速的方式来获得相同的结果,好不好?

I get a "cryptographically secure random number" I want know if the above code is fast compared with "Random.Next()" and if there is some fast way to get same results, ok?

推荐答案

要回答你的问题可能是把你的问题倒挂的最简单方法。

The simplest way to answer your question might be to turn your question upside down.

假设的是,实施CryptoServiceProvider持有的所有优点。这是一样快,并且使用同样少的内存Random.Next。

Assume that the CryptoServiceProvider implementation holds all the advantages. It is just as fast and uses just as little memory as Random.Next.

那么,为什么这两种方案的存在的?
为什么我们连的的Random.Next的框架?

Then why do both implementations exist? Why do we even Have Random.Next in the framework?

看看我们所知道的每一个实现。生成一个加密安全随机数,其他的不做任何承诺。

Look at what we know about each implementation. One generates cryptographically secure random number, the other makes no promises.

这是更简单呢?产生是足够随机的加密中使用的随机数,或产生数字,简单地看随机的,但并不别的保证什么?
如果有不符合生成加密安全随机数,那么相关的成本的每个的随机数生成器会做到这一点。

Which is simpler? Generating random numbers that are sufficiently random to be used in cryptography, or generating numbers that simply "look" random, but don't guarantee anything else? If there wasn't a cost associated with generating cryptographically secure random numbers, then every random number generator would do it.

您可以通常认为,标准库函数的目的是做什么它说,在包装盒上,并把它做好。 Random.Next的目的是让你在伪随机数的尽可能有效序列中的下一个随机数

You can usually assume that standard library functions are designed to do what it says on the box and do it well. Random.Next is designed to get you the next random number in a sequence of pseudo-random numbers as efficiently as possible.

CryptoServiceProvider被设计为产生足够强的加密中使用的随机数,然后执行的的尽可能高效。如果有一种方法来尽可能高效地Random.Next,然后Random.Next会用它做的

CryptoServiceProvider is designed to generate random numbers strong enough to be used in cryptography, and do that as efficiently as possible. If there was a way to do this as efficiently as Random.Next, then Random.Next would use it too.

您的问题似乎假定的框架设计的部分脑损伤 - 它们在某种程度上设计了一个不必要的慢函数生成加密安全随机数,即使有一个更快的方法

Your question seems to assume brain damage on the part of the framework designers - that they somehow designed a needlessly slow function to generate cryptographically secure random numbers, even though there was a faster way.

要生成加密安全随机数的最快方法是最有可能调用由专家设计的函数生成加密安全随机数。

The fastest way to generate cryptographically secure random numbers is most likely to call the function designed by experts to generate cryptographically secure random numbers.

这篇关于最快的实现在C#中一个真正的随机数发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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