isUniformRNG

Joseph Rushton Wakeling via Digitalmars-d digitalmars-d at puremagic.com
Sun May 4 08:38:42 PDT 2014


On 04/05/14 16:28, Nick Sabalausky via Digitalmars-d wrote:
> On a general level, I'm trying to grok the whole intent of isUniformRNG and see
> whether or not anything else may ever be needed in addition to isUniformRNG. I'm
> not trying to push an "isRNG", just trying to understand std.random's current
> intentions and reasoning, so I know how to work with it appropriately.

Yea, it's a good question.  I think I would answer it as follows.

 From a design point of view, it's helpful to distinguish between code that acts 
as a fundamental source of (usually pseudo-) randomness, versus stuff that 
transforms the output of those random sources into other forms (say, 
uniformly-distributed floating point numbers, or rolls of a 6-sided die, or 
permutations of an array, or whatever else).

In other words, it's helpful to distinguish between _generators_ of randomness, 
versus _users_ of randomness.

Now, in principle, there are many different kinds of random generator available, 
not all of them drawing from a uniform distribution.  But again, from a design 
point of view, it's helpful to constrain the range of functionality assumed, and 
uniformly-distributed random numbers are a very good foundation from which to 
build, because they can reliably be used to generate numbers drawn from any 
other probability distribution.

On top of that, it's useful to have the constraint of a distribution on a closed 
interval that has explicitly included minimum and maximum values, because that 
greatly simplifies the checks and assumptions one has to make in writing 
functions that make use of the random generator.

Hence you come to this definition of a "uniform random number generator", which 
is the fundamental building block of all other random-number functionality.
What the isUniformRNG template is _really_ asking is, "Is this going to give me 
a valid source of randomness to plug in to all the other 
random-number-generation functions, so that they'll do what I expect?"

Simple example of how that can be important: suppose you have a random generator 
that gives you uniform numbers in the half-open interval [min, max).  Now run 
that through std.random.uniform!"[]"().  You will not in fact get random numbers 
drawn from the closed interval you have specified.

> But more immediately, since Phobos lacks a crypto-secure RNG, I'm implementing
> NIST's Hash_DRBG (backed by the OS-provided RtlGenRandom/CryptGenRandom and
> "/dev/random" as entropy sources). Hopefully I can get it into a
> Phobos-acceptable state.


That's great -- I really look forward to seeing your work :-)  Can you give me 
some links to the spec?

> Now, I can follow the spec for the Hash_DRBG algorithm well enough, but I'm not
> really solid on random-number theory, so I can't be certain whether or not
> isUniformRNG is appropriate for this. I would assume "yes", but I don't want to
> assume. Hence my inquiries.

I don't know that this necessarily needs too much in the way of random-number 
theory, but there are 3 questions that really need answering here:

    * What's the type of number that this algorithm produces?

    * Are these numbers uniformly distributed?

    * What's the min and max values produced, and are these
      inclusive bounds, i.e. are the random numbers drawn
      from the closed [min, max] interval?

Can you clarify?


More information about the Digitalmars-d mailing list