Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which PRNG is suited for a functional usage?

This question is motivated by usage of a PRNG in Scala, but the answer can very well be language agnostic.

Problem

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.

What I need

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?

like image 397
ziggystar Avatar asked Nov 24 '13 11:11

ziggystar


People also ask

What is PRNG used for?

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.

What is ANSI X9 17 PRNG?

... 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.

What is the name of the PRNG used in Windows?

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.

What makes a PRNG good?

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.


1 Answers

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.

like image 171
Ryan Culpepper Avatar answered Oct 04 '22 19:10

Ryan Culpepper