Task

(Given an equal-probability generator of one of the integers 1 to 5
as dice5),   create dice7 that generates a pseudo-random integer from
1 to 7 in equal probability using only dice5 as a source of random
numbers,   and check the distribution for at least one million calls using the function created in   Simple Random Distribution Checker.


Implementation suggestion:
dice7 might call dice5 twice, re-call if four of the 25
combinations are given, otherwise split the other 21 combinations
into 7 groups of three, and return the group index from the rolls.

(Task adapted from an answer here)

defmodule Dice do
  def dice5, do: :rand.uniform( 5 )
 
  def dice7 do
    dice7_from_dice5
  end
 
  defp dice7_from_dice5 do
    d55 = 5*dice5 + dice5 - 6           # 0..24
    if d55 < 21, do: rem( d55, 7 ) + 1,
                 else: dice7_from_dice5
  end
end
 
fun5 = fn -> Dice.dice5 end
IO.inspect VerifyDistribution.naive( fun5, 1000000, 3 )
fun7 = fn -> Dice.dice7 end
IO.inspect VerifyDistribution.naive( fun7, 1000000, 3 )