Question

I'm a bit surprised by a sentence found in the book "Clojure Programming" (1st [and only as I write this!?] edition), page 78:

It should be obvious that it's impossible to deterministically test a function that depends upon a random number generator

Well, to me it's not obvious at all.

First in the context the book definitely talks about a PRNG (that sentence just follows a mention of java.util.Random, which is a PRNG), not a real random source.

Seen that functional languages that do support higher-order function can be, well, passed functions... Why not simply pass the random generator function itself to the function you want to be able to deterministically test?

If I've got something like the following function (it's just an example) and the PRNG itself is deterministic, why would it be impossible to deterministically test the following function:

(defn shuffle-deck [deck  last-generated-number   prngf]
      ...)

I realize that instead of keeping a (global?) reference to your random generator, you need to keep a reference to the last generated number (and update it accordingly) but that's basically it.

You can then test/reproduce the behaviour of any function using a PRNG as long as you pass the PRNG and 'the random number you're currently at'.

It's not unlike deterministic game engines (like Blizzard's Warcraft III) where the replays only contains the player inputs, the time at which they happened and the overall seed to the PRNG.

Basically I'm a bit confused: I've always seen PRNG as something that is not "real randomness" and I've got the impression that functional languages accepting higher-order function make it particularly easy to reproduce behavior of functions depending on PRNG.

So, is that sentence of the book (which I found a great book btw) complete rubbish? I sure do not find it "obvious" at all...

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top