Sometimes you need true random numbers. True random numbers are, in contrast to numbers generated by a pseudo-random number generator (PRNG), really random, i.e. there are no patterns and they cannot be predicted.
Pseudo-random number generators are functions that compute random-looking sequences of numbers. For most cases this is sufficient, but some applications need numbers that are really random.
You need true random numbers for example in the field of cryptography, game-playing and various scientific calculations.
is completely free open-source. You are allowed to do anything with it what you want. But it would be nice if you mention me (Dominik Reichl) somewhere in the docs of your application :-) But because it is completely free, there also is no warranty of any kind. Use it on your own risk.
uses the online service provided by random.org (thanks random.org!). Random.org is an online true random number generation service. The true random numbers are generated using atmospheric noise.
The class uses the
function of the UrlMon.dll
. This DLL is a library which gets installed with Internet Explorer 3.0 or later. So IE must be installed. The Internet Explorer handles the complete download: it uses the default proxy if needed, establishes a connection to the server, it creates a file and stores the downloaded stream into it. The
returns the filename of the cached file.
reads that file and deletes it when finished.
Because of speed reasons,
currently downloads 8192 random bytes at once. You can now call the
function 8192 times before the cached file gets refreshed automatically, or 4096 words, or 2048 dwords, or 1024 dwords plus 2048 words, etc. So the cached file gets completely used and
doesn't download random bytes each
Because we use an online service, the speed of the random number class depends on the speed of the internet connection. If you have ADSL you won't have any problems getting 5 MB random bytes in a few seconds. If you know that the end-user only has a low speed internet connection and need many random bytes, consider downloading only a few true random bytes and using them as seed to a pseudo-random number generator.
class is very easy. Here's a sample snippet:
if(rnd.Initialize() == false)
MessageBox("Initialization of CTrueRandom class failed!","Error",0);
BYTE b = rnd.GetRandomByte();
WORD w = rnd.GetRandomWord();
DWORD dw = rnd.GetRandomDWord();
Public function summary:
Before you can use any of the
functions you must initialize the class using the
function. This function tries to establish a connection to random.org and get some random numbers to a cache. If it cannot connect to the server, it returns false. If it successfully downloaded some random bytes to the cache, it returns true.
void CTrueRandom::GetRandomBuffer(BYTE *pBuf, DWORD dwBufferSize)
This function fills the buffer pBuf with
random bytes. If there aren't enough random bytes in the cache, it automatically connects to the server again and gets some new ones.
Various wrappers for the
function. It should be clear what they are doing.
When you don't need random numbers any more, call the
function. It will free allocated memory and loaded libraries.