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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Verify distribution uniformity/Naive step by step in the JavaScript 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 JavaScript programming language

The provided JavaScript code defines a function called distcheck that checks the distribution of a random function against a target distribution. It takes the following arguments:

  • random_func: A function that generates random values.
  • times: The number of times to call the random function and collect values.
  • opts: An optional object containing settings for the distribution check. The opts object has the following default properties:
    • delta: The acceptable percentage deviation from the target distribution. The default value is 2%.

The distcheck function works as follows:

  1. Initializes an empty object count to store the count of each unique value generated by the random function and an empty array vals to store the unique values.
  2. Calls the random_func function times times and updates the count object and the vals array accordingly.
  3. Sorts the vals array in ascending order.
  4. Calculates the target count (target) as the number of times divided by the number of unique values (vals.length).
  5. Calculates the tolerance as a percentage deviation from the target (tolerance), using the opts['delta'] setting.
  6. Iterates through the vals array and checks if the absolute difference between the count of each value and the target is greater than the tolerance.
  7. If the difference is greater than the tolerance, it throws an error.
  8. Otherwise, it prints the value and its count.

The code also defines a helper function called has_property that checks if an object has a specific property and returns true if it exists, or false otherwise.

In the main part of the code, there are two try-catch blocks. The first block calls distcheck with a random function that generates numbers between 0 and 9 and runs it 100,000 times. This checks if the distribution of the generated numbers is roughly uniform.

The second block calls distcheck with a random function that generates 1 with a probability of 0.95 and 0 with a probability of 0.05, and also runs it 100,000 times. This checks if the distribution of the generated numbers is roughly biased towards 1.

If any of the distribution checks fail, an error is thrown and printed.

Source code in the javascript programming language

function distcheck(random_func, times, opts) {
    if (opts === undefined) opts = {}
    opts['delta'] = opts['delta'] || 2;

    var count = {}, vals = [];
    for (var i = 0; i < times; i++) {
        var val = random_func();
        if (! has_property(count, val)) {
            count[val] = 1;
            vals.push(val);
        }
        else
            count[val] ++;
    }
    vals.sort(function(a,b) {return a-b});

    var target = times / vals.length;
    var tolerance = target * opts['delta'] / 100; 

    for (var i = 0; i < vals.length; i++) {
        var val = vals[i];
        if (Math.abs(count[val] - target) > tolerance) 
            throw "distribution potentially skewed for " + val +
                  ": expected result around " + target + ", got " +count[val];
        else
            print(val + "\t" + count[val]);
    }
}

function has_property(obj, propname) {
    return typeof(obj[propname]) == "undefined" ? false : true;
}

try {
    distcheck(function() {return Math.floor(10 * Math.random())}, 100000);
    print();
    distcheck(function() {return (Math.random() > 0.95 ? 1 : 0)}, 100000);
} catch (e) {
    print(e);
}


  

You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Tcl programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Action! programming language
You may also check:How to resolve the algorithm Plasma effect step by step in the Go programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Mercury programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the Maple programming language