How to resolve the algorithm Verify distribution uniformity/Naive step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Verify distribution uniformity/Naive step by step in the OCaml programming language

Table of Contents

Problem Statement

This task is an adjunct to Seven-sided dice from five-sided dice.

Create a function to check that the random integers returned from a small-integer generator function have uniform distribution.

The function should take as arguments:

The function should produce:

Show the distribution checker working when the produced distribution is flat enough and when it is not. (Use a generator from Seven-sided dice from five-sided dice).

See also:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Verify distribution uniformity/Naive step by step in the OCaml programming language

Source code in the ocaml programming language

let distcheck fn n ?(delta=1.0) () = 
  let h = Hashtbl.create 5 in
  for i = 1 to n do
    let v = fn() in
    let n = 
      try Hashtbl.find h v 
      with Not_found -> 0 
    in
    Hashtbl.replace h v (n+1)
  done;
  Hashtbl.iter (fun v n -> Printf.printf "%d => %d\n%!" v n) h;
  let target = (float n) /. float (Hashtbl.length h) in
  Hashtbl.iter (fun key value ->
    if abs_float(float value -. target) > 0.01 *. delta *. (float n)
    then (Printf.eprintf
      "distribution potentially skewed for '%d': expected around %f, got %d\n%!"
       key target value)
  ) h;
;;


  

You may also check:How to resolve the algorithm HTTP step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the ChucK programming language
You may also check:How to resolve the algorithm Elliptic Curve Digital Signature Algorithm step by step in the Phix programming language
You may also check:How to resolve the algorithm Factorial step by step in the BASIC programming language