tags:

views:

1063

answers:

9

I know that the Random class can generate pseudo-random numbers but is there a way to generate truly random numbers?

+22  A: 

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.

Noldorin
Thanks for the tip, I'll try the RNGCryptoServiceProvider. I might try some nuclear decay as well...
Max
See http://japikse.blogspot.com/2008/10/random-numbers-in-c.html for a good example
Dan Diplo
@Max: Hehe... I should note that a significantly easier method would be to monitor CPU temperature, since that's the most readily available source. But yeah, do check out the `RNGCryptoServiceProvider` first. If you care to describe the context, I can confirm whether this will be sufficient for your usage.
Noldorin
I was trying random.org and got 33 twice in a row. That's one *very* random coincidence! :)
Douglas Tosi
@Douglas: Yep, though it doesn't in itself indicate that the generator is one bit less random. ;) As Dan Diplo pointed out, see http://xkcd.com/221/ - it's perfectly correct.
Noldorin
@Dogulas - That's not THAT hard. The chance of getting the same number 2 times in a row (assuming you used the 1-100 generator on the front page) is 1%
ryeguy
@Douglas - If you *never* got repeating numbers, that would be a sign of less-than-total randomness.
Jim Raden
I just tried that generator on the front page, and I got 25 followed by 43. Two *completely unrelated numbers*, how unlikely is that!? This kind of amazing occurrence sends shivers down my spine.
Daniel Earwicker
"Technically a PRNG". That's _not_ a technicality - there are fundamental differences between using a real RNG and a PRNG.
Nick Johnson
@Nick: I say technically simply because PRNGs are often referred to just as RNGs... but yeah, I think I've made a clear distinction between the two sorts and their methods.
Noldorin
+8  A: 

short answer: It is not possible to generate TRULY RANDOM NUMBERS using only C# and a computer..

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 your random numbers (which could be accomplished using C# and some P/Invoke)

Miky Dinescu
+1 - gets to the essence of the problem. By definition, nothing geneerated by purely algorithmic means can be truly random. You need an external source of analog randomness such as a device that measures cosmic background radiation.
ConcernedOfTunbridgeWells
Very interesting to learn that a computer is not capable of producing something that seems so simple. Couldn't they use other elements like CPU temperature?
Max
@Max: You've hit it spot on. See my comment on my own post regarding that. CPU temperature is still far from accessible on all machines however - it might be a good solution for server devices.
Noldorin
@Max: Randomness is one of those things that is elusive because it seems simple enough as a concept but the task of generating it (artificially - or mathematically) is terribly complicated if not impossible
Miky Dinescu
But the statement that it is impossible using C# and a "Computer" is not true. The COmputer is a real physical system and has a lot of randomness in its operation. Just need the write analog monitoring device to capture it - e.g. You could point the inbuilt mike at the fan and catch some noise.
whatnick
+1  A: 

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.

sixfoottallrabbit
A: 

Years ago i heard of a fairly simple method that increases the randomness of standard algorithms (provided by built-in libraries and not taking external input.)

This is the process:

  • Create a long array (1000 or more items?) of numbers
  • Populate the array with 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 random number at the array index to replace the number used

This ought to dramatically improve the randomness of your results without the need for external input.

EDIT: 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

-paul

Paul Sasik
I don't think it works that way.
ryeguy
@ryeguyThat's exactly how it works. Here's a detailed explanation with C++ code:http://www.boost.org/doc/libs/1_39_0/libs/random/random-generators.html
Paul Sasik
+1, because this doesn't deserve a negative score. The suggested method may not incerase quality of randomness as much as using a better algorithm would, but it's still a fair point.
Noldorin
A: 

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.

apocalypse9
This likely has all the same attack vectors as seeding from a timestamp.
Bob Aman
+1  A: 

As John von Neumann joked, "Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."

Regards

MArk

High Performance Mark
+1  A: 

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

Lavarand

Moose
A: 

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 (e.g. 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.

Jason S
A: 

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.

Fan speeds too.
Bob Aman