[Solved] Let’s create a RandomFunction using 0 and 1


The FastDiceRoller algorithm described in https://arxiv.org/pdf/1304.1916v1.pdf gives you a random uniform(ish) distribution.

Here’s an implementation ripped from that paper using your function:

function fastDiceRoller(max_number){

  v = 1
  c = 0

  while(true){

      v = 2*v
      c = 2*c + get_zero_or_one()

      if(v >= max_number){

          if(c < max_number) { return(c) }

          v = v - max_number
          c = c - max_number
      }      
  }
}

Here be some frequencies from a million runs:

Map {
  0 => 100327,
  1 => 99930,
  2 => 100389,
  3 => 99824,
  4 => 100116,
  5 => 99999,
  6 => 99700,
  7 => 99957,
  8 => 99980,
  9 => 99778 }

Hope that helps.

Essentially it uses your function to build a binary number that is less than max_number.

5

solved Let’s create a RandomFunction using 0 and 1