Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C++
Article

CTrueRandom - Getting True Random Numbers

Rate me:
Please Sign up or sign in to vote.
3.68/5 (22 votes)
13 Oct 20033 min read 125.2K   2.5K   38   18
The CTrueRandom class gets true random numbers for you.

Sample Image - truerandom.jpg

Introduction

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.

CTrueRandom 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.

How CTrueRandom works

CTrueRandom 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 URLDownloadToCacheFile 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 URLDownloadToCacheFile returns the filename of the cached file. CTrueRandom reads that file and deletes it when finished.

Because of speed reasons, CTrueRandom currently downloads 8192 random bytes at once. You can now call the GetRandomByte 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 CTrueRandom doesn't download random bytes each GetRandomXXX call.

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.

Usage

Using the CTrueRandom class is very easy. Here's a sample snippet:
CTrueRandom rnd;
BYTE pArray[2000];

if(rnd.Initialize() == false)
{
    MessageBox("Initialization of CTrueRandom class failed!","Error",0);
    return;
}

// Get 2000 random bytes
rnd.GetRandomBuffer(pArray, 2000);

BYTE b = rnd.GetRandomByte(); // Get a random byte
WORD w = rnd.GetRandomWord(); // Get a random word
DWORD dw = rnd.GetRandomDWord(); // Get a random dword

rnd.Close(); // We don't need true random numbers any more
Public function summary:
bool CTrueRandom::Initialize()
Before you can use any of the GetRandomXXX functions you must initialize the class using the Initialize 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 dwBufferSize random bytes. If there aren't enough random bytes in the cache, it automatically connects to the server again and gets some new ones.

BYTE CTrueRandom::GetRandomByte()
WORD CTrueRandom::GetRandomWord()
DWORD CTrueRandom::GetRandomDWord()
Various wrappers for the GetRandomBuffer function. It should be clear what they are doing.

void CTrueRandom::Close()
When you don't need random numbers any more, call the Close function. It will free allocated memory and loaded libraries.

History

  • 14 Oct 2003 - 1.0
    • First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Unknown
Dominik started programming in Omikron Basic, a programming language for the good old Atari ST. After this, there was some short period of QBasic programming on the PC, but soon he began learning C++, which is his favorite language up to now.

Today, his programming experience includes C / C++ / [Visual] C++ [MFC], C#/.NET, Java, JavaScript, PHP and HTML and the basics of pure assembler.

He is interested in almost everything that has to do with computing; his special interests are security, cryptography and data compression.

You can find his latest freeware, open source projects and articles on his website: https://www.dominik-reichl.de/.

Comments and Discussions

 
Questionthe mystry question... Pin
wiseguycool5-Jul-11 16:25
wiseguycool5-Jul-11 16:25 
GeneralGood work Pin
Bulky Fellow27-Apr-07 0:12
Bulky Fellow27-Apr-07 0:12 
GeneralTrue Random Numbers Pin
Jeffrey Walton2-Jan-07 23:34
Jeffrey Walton2-Jan-07 23:34 
QuestionAre there realy random ? Pin
Melescher14-Oct-05 23:17
Melescher14-Oct-05 23:17 
GeneralGetRandomFloat Pin
sdaymond7-Feb-05 6:05
sdaymond7-Feb-05 6:05 
GeneralError Initialize Pin
Catania20-Oct-03 22:43
Catania20-Oct-03 22:43 
GeneralRe: Error Initialize Pin
Dominik Reichl21-Oct-03 0:35
Dominik Reichl21-Oct-03 0:35 
GeneralFlawed concept Pin
W2k20-Oct-03 19:22
W2k20-Oct-03 19:22 
GeneralRe: Flawed concept Pin
kozlowski21-Oct-03 23:56
kozlowski21-Oct-03 23:56 
GeneralRe: Flawed concept Pin
__PPS__12-Dec-03 13:33
__PPS__12-Dec-03 13:33 
AnswerRe: Flawed concept Pin
Lord Sy22-Dec-06 6:30
Lord Sy22-Dec-06 6:30 
GeneralRandom.org Pin
William Bartholomew14-Oct-03 12:00
William Bartholomew14-Oct-03 12:00 
GeneralRe: Random.org Pin
Hugo Hallman15-Oct-03 0:44
Hugo Hallman15-Oct-03 0:44 
GeneralRe: Random.org Pin
Hugo Hallman15-Oct-03 0:45
Hugo Hallman15-Oct-03 0:45 
GeneralGreat Pin
Juan Carlos Cobas14-Oct-03 9:06
Juan Carlos Cobas14-Oct-03 9:06 
GeneralLooks way too complex Pin
Rama Krishna Vavilala14-Oct-03 6:20
Rama Krishna Vavilala14-Oct-03 6:20 
GeneralRe: Looks way too complex Pin
Dominik Reichl14-Oct-03 7:18
Dominik Reichl14-Oct-03 7:18 
Rama Krishna wrote:
CryptGenRandom looks a safer alternative as it doesnot need any internet connection

I assume you meant simpler alternative Wink | ;)

Rama Krishna wrote:
the numbers are cryptographically random

That's correct. It's very easy to produce cryptographically secure random numbers.

For example, just collect some random-looking data (tick count, cpu usage, mouse position, keypresses), use a hash like SHA-1, and hash the data with an incrementing counter until you have generated enough bytes.

This method is cryptographically secure (for sure, only when the hash algorithm is secure). But the numbers you've generated aren't true random numbers.



_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do?? Wink | ;)
(doesn't work on NT)

GeneralRe: Looks way too complex Pin
Blake Coverett14-Oct-03 9:07
Blake Coverett14-Oct-03 9:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.