This question is motivated by usage of a PRNG in Scala, but the answer can very well be language agnostic.
I want to have a functional interface to my PRNG. Currently the PRNG implementations I know (Java stdlib, Scala stdlib, commons math) are OO, in the sense that the PRNG is an object with mutable state. But I prefer a purely functional PRNG, which mainly has the following method:
def nextInt(state: S): (Int,S)
Here, S
is the internal state of the PRNG (call it seed or whatever) and the method returns the desired random value plus the modified state.
Best would be an implementation. But I can do this easily by myself. A very simple implementation using Java's built-in PRNG would be:
def nextInt(state: Int): (Int,Int) = {
val rng = new Random(state)
(rng.nextInt(),rng.next())
}
or a bit more dangerous and less wasting
def nextInt(state: Random): (Int, Random) = {
val newRng = state.clone
(newRng.nextInt(),newRng)
}
What I truly need is a PRNG algorithm with good quality, small state and quick calculation. Mersenne Twister has a state of 600+ bytes. Note, that the state has to be copied in every step. So, is there something smaller?
Is there some kind of comparison of PRNG algorithms?
Pseudo Random Number Generator(PRNG) refers to an algorithm that uses mathematical formulas to produce sequences of random numbers. PRNGs generate a sequence of numbers approximating the properties of random numbers.
... are many types of pseudo-random number generators (PRNGs) that depend on the cryptographic applications such as: cyclic encryption, DES output feedback mode, Blum Blum Shub generator, and ANSI X9. 17 pseudo-random number generators. One of the strongest pseudorandom number generator is specified in ANSI X9.
The PseudoRandom Number Generator (PRNG) used by the Windows operating system is the most commonly used PRNG. The pseudorandomness of the output of this generator is crucial for the security of almost any application running in Windows.
There are four properties any good PRNG should have: Uncorrelated Sequences – No sequence of any given link should be correlated to any other sequence of the algorithms output. One cannot take a given stretch of numbers (say 16 bits) and use that to predict subsequent bits.
You don't need a functional implementation to get a functional interface.
You could represent your function PRNG as just a Stream of numbers generated by the Java PRNG---or any other stateful PRNG. As you explore the stream, it automatically calls the Java PRNG and remembers the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With