Click here to Skip to main content
13,247,704 members (17,111 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 17 Aug 2014

Nondeterministic Random Number Generation

, 18 Nov 2014
Rate this:
Please Sign up or sign in to vote.
I keep reading articles on the web about how all random number generators on a computer are "pseudo" random, or "deterministic". Here I present an approach for nondeterministic number generation.


The provided C# WPF .NET solution generates truly random numbers in a nondeterministic manner. These are most useful in cryptography and help protect against side-channel attacks. Although this solution was developed targeting an Intel multi-processor, targeting a machine with a single processor may produce different results.

What is a random number sequence?

For example (as the editor was kind enough to point out):

"314159......." is an example of the output from a deterministic routine for producing random numbers (if indeed, it is using an algorithm for producing digits of the value of pi). Most random number generators are of these types; they use a specific mathematical formula and usually require a "seed" value (quite commonly the system timer is used).

What are the problems with using deterministic routines for producing random values? In cryptography, if the algorithm used is already known, then the random values can be reproduced just by knowing what the initial seed of the generator is. Why is this a problem? Well, it's quite common to use random number generators for generating secret "keys".

So how do we create nondeterministic random values? Quite simply, we have to resort to either non-algorithmic approaches (such as white noise sampling) or producing our own nondeterministic algorithm.

From the Wiki article:

"A concurrent algorithm can perform differently on different runs due to a race condition."

Here, I present a class which does just that:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace randTesting
    /// <summary>
    /// Random byte generator
    /// </summary>
    public class RandomBytes
        static volatile int v = 0;
        /// <summary>
        /// Creates the requested number of random bytes
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<byte> GenerateBytes(int count)
            List<byte> resbytes = new List<byte>();
            List<Thread> threadList = new List<Thread>();

            //get the number of processors on this os
            var pcount = Environment.ProcessorCount == 1 ? 2 : Environment.ProcessorCount;

            //initialize our bit value
            v = 1;

            //create a few threads
            for (int i = 0; i < pcount; i++)
                var ts = new ThreadStart(mashup);
                var thd = new Thread(ts);

            //wait on them to start up
            while (threadList.Any(a => a.ThreadState != ThreadState.Running));

            string bts = "";

            while (count > 0)
                int res = 0;
                if (v == 1) res = 1;
                bts += res;
                if (bts.Length == 8)
                    int val = 0;
                    for (int x = 0; x < 8; x++)
                        val += (int)(Math.Pow((double)2, (double)x) * int.Parse(bts.Substring(x, 1)));
                    bts = "";

                    //add the byte value

            //thread cleanup
            threadList.ForEach(thd =>
                    catch { }

            return resbytes;

        static void mashup()
            while (true)
                v *= -1;


I've often wondered about the ability of a computer to generate truly random values. One approach that came to mind was to cause the processors to constantly "fight" over the same shared memory until paused and read. This is known as creating a "race condition". If sufficient amount of time is allowed to pass, the value in the shared memory location should be unknown, or random, since only the OS controls how much time is allotted to each processor.

Using the Code

The RandomBytes.cs file contains method "GenerateBytes" which returns the specified number of random bytes requested.

var bytes = RandomBytes.GenerateBytes(20);

Here's an example of 100 random bytes generated using this call:


Points of Interest

  1. The pause (of 5 milliseconds) while looping and sampling the variable (v) might need to be adjusted depending on the speed of the target machine.
  2. This processor: Intel Core I7, 2201 Mhz, 4 Cores
  3. Used to seed deterministic (or pseudo random number generators)
  4. Aperiodic
  5. Poor Efficiency

A lot of people seem confused by all of the different random number generators available - and with good reason, too. Most RNGs (and apparently, some GUID generators) work by seeding themselves (a.k.a. conditioning) with a nondeterministic number generator and then are used to produce deterministic values (for a more efficient and perfect distribution). For the nondeterministic part, most use the mashing of a high speed timer and/or X and Y movements of the mouse. Some even use hashing of screen captures.

Also, I get a lot of questions about what Microsoft does. So, as for Microsoft's cryptographic random number generator:[^]

Notice that the algorithm (or DETERMINISTIC) part isn't even published. However, the timer mashing (or NONDETERMINISTIC) part is clearly hinted at.

Thank you everybody for your comments and for helping me build a better article.

Some References


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSleep itself is non-deterministic? Pin
sbarnes9-Dec-15 18:05
membersbarnes9-Dec-15 18:05 
AnswerRe: Sleep itself is non-deterministic? Pin
Cryptonite10-Dec-15 8:51
memberCryptonite10-Dec-15 8:51 
GeneralAwesome! my vote of 5 Pin
JustCodeForFun26-Nov-14 12:53
memberJustCodeForFun26-Nov-14 12:53 
GeneralRe: Awesome! my vote of 5 Pin
Cryptonite27-Nov-14 9:53
memberCryptonite27-Nov-14 9:53 
SuggestionToo slow for no gain Pin
ArchAngel12319-Nov-14 11:03
memberArchAngel12319-Nov-14 11:03 
GeneralRe: Too slow for no gain Pin
Cryptonite19-Nov-14 11:09
memberCryptonite19-Nov-14 11:09 
QuestionIs GUID generation nondeterministic? Pin
fredatcodeproject19-Nov-14 6:20
memberfredatcodeproject19-Nov-14 6:20 
AnswerRe: Is GUID generation nondeterministic? Pin
ArchAngel12319-Nov-14 11:00
memberArchAngel12319-Nov-14 11:00 
AnswerRe: Is GUID generation nondeterministic? Pin
Cryptonite19-Nov-14 11:02
memberCryptonite19-Nov-14 11:02 
GeneralRe: Is GUID generation nondeterministic? Pin
ArchAngel12319-Nov-14 11:11
memberArchAngel12319-Nov-14 11:11 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 18 Nov 2014
Article Copyright 2014 by Cryptonite
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid