简体   繁体   中英

How can I generate truly (not pseudo) random numbers with C#?

我知道Random类可以生成伪随机数,但有没有办法生成真正的随机数?

The answer here has two main sides to it. There are some quite important subtleties to which you should pay due attention...

The Easy Way (for simplicity & practicality)

The RNGCryptoServiceProvider , which is part of the Crypto API in the BCL, should do the job for you. It's still technically a pseudo-random number generated, but the quality of "randomness" is much higher - suitable for cryptographic purposes, as the name might suggest.

There are other crypographic APIs with high quality pseudo random generaters available too. Algorithms such as the Mersenne twister are quite popular.

Comparing this to the Random class in the BCL, it is significantly better. If you plot the numbers generated by Random on a graph, for example, you should be able to recognise patterns, which is a strong sign of weakness. This is largely due to the fact that the algorithm simply uses a seeded lookup table of fixed size.

The Hard Way (for high quality theoretical randomness)

To generate truly random numbers, you need to make use of some natural phenomenon, such as nuclear decay, microscopic temperature fluctuations (CPU temperature is a comparatively conveient source), to name a few. This however is much more difficult and requires additional hardware, of course. I suspect the practical solution ( RNGCryptoServiceProvider or such) should do the job perfectly well for you.

Now, note that if you really do require truly random numbers , you could use a service such as Random.org , which generates numbers with very high randomness/entropy (based on atmospheric noise ). Data is freely available for download. This may nonetheless be unnecessarily complicated for your situation, although it certainly gives you data suitable for scientific study and whatnot.

The choice is yours in the end, but at least you should now be able to make an informative decision, being aware of the various types and levels of RNGs.

short answer: It is not directly possible to generate TRULY RANDOM NUMBERS using only C# (ie using only a purely mathematical construction).

long(er) answer: Only by means of employing an external device capable of generating "randomness" such as a white noise generator or similar - and capturing the output of that device as a seed for a pseudo random number generator (PRG). That part could be accomplished using C#.

True random numbers can only be generated if there is a truly random physical input device that provides the seed for the random function.

Whether anything physical and truly random exists is still debated (and likely will be for a long time) by the science community.

Psuedo-random number generators are the next best thing and the best are very difficult to predict.

正如约翰·冯·诺伊曼开玩笑说的那样,“任何考虑产生随机数字的算术方法的人当然都处于犯罪状态。”

The thread is old and answered, but i thought I'd proceed anyway. It's for completeness and people should know some things about Random in c#.

As for truly random, the best you can ever hope to do is use a "secure Pseudo Random Generator" like salsa20 or RC4 (sort of, sometimes). They pass a barrage of tests where "efficient" adversaries try to distinguish them from random. This comes with certain costs and is probably unnecessary for most uses.

The random class in c# is pretty good most of the time, it has a statically distribution that looks random. However the default seed for random() is the system time. So if you take lots of randoms at the "same time" they are taken with the same seed and will be the same ("random" is completely deterministic, don't let it fool you). Similar system time seeds also may produce similar numbers because of random class's shortcomings. The way to deal with this is to set you own seeds, like

Random random = new Random((int)DateTime.Now.Ticks & (0x0000FFFF + x));

where x is some value you increment if you've created a loop to get a bunch of random numbers, say.

Also with c# random extensionsto your new variable like NextDouble() can be helpful in manipulating the random numbers, in this case crow-baring them into interval (0,1) to become unif(0,1), which happens is a distribution you can plug into stat formulas to create all the distributions in statistics.

Take a look at using an algorithm like Yarrow or Fortuna with entropy accumulation. The point with these algorithms is that they keep track of entropy as a measure of theoretical information content available for predicting future numbers by knowing the past numbers and the algorithms used to produce them; and they use cryptographic techniques to fold new entropy sources into the number generator.

You'll still need an external source of random data (eg hardware source of random numbers ), whether that's time of keystrokes, or mouse movement, or hard disk access times, or CPU temperature, or webcam data, or stock prices, or whatever -- but in any case, you keep mixing this information into the entropy pools, so that even if the truly random data is slow or low quality, it's enough to keep things going in an unpredictable fashion.

This code will return you a random number between min and max :

private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public int RandomNumber(int min, int max)
{
    lock (syncLock)
    { // synchronize
        return random.Next(min, max);
    }
}

Usage:

int randomNumber = RandomNumber(0, 10); // a random number between 1 and 10

There is no way to generate truly random numbers with a computer. True randomness requires an external source that monitors some natural phenomenon.

That said, if you don't have access to such a source of truly random numbers you could use a "poor man's" process like this:

  • Create a long array (10000 or more items?) of numbers
  • Populate the array with current time-seeded random numbers the standard way
  • When a random number is required, generate a random index into the array and return the number contained at that position
  • Create a new, current time-seeded random number at the array index to replace the number used

This two-step process should improve the randomness of your results somewhat without the need for external input.

Here's a sample library that implements the above-described algorithm in C++: http://www.boost.org/doc/libs/1_39_0/libs/random/random-generators.html

I was debating building a random number generator based off twitter or one of the other social networking sites. Basically use the api to pull recent posts and then use that to seed a high quality pseudo random number generator. It probably isn't any more effective than randomizing off the timer but seemed like fun. Besides it seems like the best use for most of the stuff people post to twitter.

I always liked this idea, for the retro 60s look:

Lavarand

There is no "true" random in computers, everything is based on something else. For some (feasible) ways to generate pseudorandom data, try something such as a pool of the HD temp, CPU temp, network usage (packets/second) and possibly hits/second to the webserver.

Just to clarify everyone saying that there is no True RNG available in C# or on your computer is mistaken. A multi-core processor is inherently a True RNG. Very simply by taking advantage of processor spin you can generate bools that have no discernible pattern. From there you can generate whatever number range you want by using the bools as bits and constructing the number by adding the bits together.

Yes this is magnitudes slower than a purely mathematical solution but a purely mathematical solution will always have a pattern.

public static bool GenerateBoolean()
{
    var gen1 = 0;
    var gen2 = 0;
    Task.Run(() =>
    {
        while (gen1 < 1 || gen2 < 1)
            Interlocked.Increment(ref gen1);
    });
    while (gen1 < 1 || gen2 < 1)
        Interlocked.Increment(ref gen2);
    return (gen1 + gen2) % 2 == 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM