|
| 1 | +# Dafny.Random |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `Dafny.Random` module provides a uniform interface to random values across target languages. |
| 6 | + |
| 7 | +``` |
| 8 | + // Return an arbitrary boolean value |
| 9 | + predicate nextBool() |
| 10 | +
|
| 11 | + // Return an arbitrary integer value in the range [0, bound) |
| 12 | + function nextInt(bound: int := 0): (value: int) |
| 13 | + ensures 0 <= value |
| 14 | + ensures bound > 0 ==> value < bound |
| 15 | +
|
| 16 | + // Return an arbitrary real value in the range [0,1) |
| 17 | + function nextReal(): (value: real) |
| 18 | + ensures 0.0 <= value < 1.0 |
| 19 | +``` |
| 20 | +To see the need for a uniform interface to probability, |
| 21 | +C# provides random integer values and Java and JavaScript provide random real values, |
| 22 | +and Dafny actually models real numbers as rationals with integral numerators and denominators |
| 23 | +of arbitrary size. |
| 24 | +This module gives one interface to these various sources of randomness. |
| 25 | +This is a simple interface to probability. |
| 26 | +For a more sophisticated treatment of probability, see |
| 27 | +the [Verified Monte Carlo (VMC)](https://github.com/dafny-lang/Dafny-VMC) library. |
| 28 | + |
| 29 | +The `Dafny.Random` module also provides a uniform interface to nondeterminism and probability. |
| 30 | +For example, `nextInt(10)` returns an arbitrary integer from [0,10), but |
| 31 | + |
| 32 | +* in a proof context, the integer is chosen nondeterministcally, and |
| 33 | +* in a compiled code context, the integer is chose probabilistically according to the uniform probability distribution. |
| 34 | + |
| 35 | +Compare this with the Dafny construct `var value: int := *;` where value is arbitrary in a proof context and |
| 36 | +constant (typically 0) in compiled code. |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +The `Random` module, like `FileIO` will not compile or run correctly without a language-specific |
| 41 | +implementation file. Implementations are currently provided for C#, Java, and JavaScript. |
| 42 | +To use `Random` in your code, you must: |
| 43 | +* `include` and `import` the `Random` module as you would any other library module |
| 44 | +* incorporate the corresponding language-specific implementation file (e.g. `Random.cs`) when building or running your program |
| 45 | + |
| 46 | +The example [random.dfy](../../examples/Random/random.dfy) in the `examples` directory |
| 47 | +shows how to use the module. |
| 48 | +From the [examples](../../examples/Random) directory, compile and run the file `random.dfy` with |
| 49 | + |
| 50 | +```bash |
| 51 | +# C# |
| 52 | +$ dafny run random.dfy --target cs --input ../../src/Random/Random.cs -- --verbose |
| 53 | + |
| 54 | +# Java |
| 55 | +$ dafny run random.dfy --target java --input ../../src/Random/Random.java -- --verbose |
| 56 | + |
| 57 | +# JavaScript |
| 58 | +$ dafny run random.dfy --target js --input ../../src/Random/Random.js -- --verbose |
| 59 | +``` |
| 60 | + |
| 61 | +If you aren't using `dafny run` to run your program, you will have to integrate the |
| 62 | +appropriate language-specific implementation file into your build system. |
0 commit comments