Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!