'Testing randomness function without intended mean

I have a program that tests a randomness function for how truly random it is:

function testRandom(randomF, tests, intendedMean){
  
  let total = 0;
  
  for(let t = 0; t < tests; t++){
    
    total+=randomF();
    
  }
  
  return Math.abs((total/tests) - intendedMean);
  
}

const randomFunc = () => { return ~~(Math.random() * 2) + 1 }
//returns a randomly choosen 1 or 2.

console.log(testRandom(randomFunc, 100, 1.5));

But is their a way to take out the intended mean and still have a relatively accurate output? My ideas so far are to create a standard deviation set up, but I'm not sure that's the right idea.



Solution 1:[1]

I've found the answer. I'm not sure if standard deviation is the correct method, but if it is:

function testRandom(func, tests) {

  let data = [];
  for (let i = 0; i < tests; i++) {
    data.push(func())
  }

  let result = 0;
  const u = data.reduce((a, b) => a + b, 0) / data.length;
  for (let d of data) {
    result += (d - u) ** 2;
  }
  return Math.sqrt(result / data.length + 1) - 1;

}

console.log(testRandom(() => {
  return Math.random()
}, 100));

This will do the job.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KoderM