Open markrtuttle opened 8 months ago
General comment on using floating point random numbers: One has to be very careful if using random floating point numbers because FP numbers have a higher resolution closer to zero, which could potentially skew the distribution. Ideally one would use library functions that directly return a random integer without going through floating point. There's a lot of subtleties that are easy to get wrong here, see the last paragraph of https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modulo_bias as an example. The whole article contains a nice list of things that can go wrong if one is not very careful around randomness.
I'd feel remiss not to add: https://xkcd.com/221/
Overview
The
Dafny.Random
module provides a uniform interface to random values across target languages.To see the need for a uniform interface to probability, C# provides random integer values and Java and JavaScript provide random real values, and Dafny actually models real numbers as rationals with integral numerators and denominators of arbitrary size. This module gives one interface to these various sources of randomness. This is a simple interface to probability. For a more sophisticated treatment of probability, see the Verified Monte Carlo (VMC) library.
The
Dafny.Random
module also provides a uniform interface to nondeterminism and probability. For example,nextInt(10)
returns an arbitrary integer from [0,10), butCompare this with the Dafny construct
var value: int := *;
where value is arbitrary in a proof context and constant (typically 0) in compiled code.Usage
The
Random
module, likeFileIO
will not compile or run correctly without a language-specific implementation file. Implementations are currently provided for C#, Java, and JavaScript. To useRandom
in your code, you must:include
andimport
theRandom
module as you would any other library moduleRandom.cs
) when building or running your programThe example
random.dfy
in theexamples
directory shows how to use the module. From theexamples
directory, compile and run the filerandom.dfy
with