Skip to content

Instantly share code, notes, and snippets.

@zaneclaes
Last active October 25, 2021 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaneclaes/7c8167aad9062d7442f062dbc8faeaec to your computer and use it in GitHub Desktop.
Save zaneclaes/7c8167aad9062d7442f062dbc8faeaec to your computer and use it in GitHub Desktop.
async function testDice(func, bytes = 2, runs = 1000) {
let sequence = '';
const vals = [];
let last = 0;
for (let i=0; i<runs; i++) {
last = await func(i, last);
let hex = last.toString(16);
while (hex.length < (bytes * 2)) hex = `0${hex}`;
for (let b=0; b<(bytes*2); b++) {
const h = hex.substr(b, 1);
const iv = parseInt(h, 16);
let bin = (iv >>> 0).toString(2);
while (bin.length < 4) bin = `0${bin}`;
sequence += bin;
}
vals.push(last);
const numMine = Math.floor(Math.random() * 10 + 2);
for (let i=0; i<numMine; i++) {
await ethers.provider.send("evm_mine");
}
}
const r = new RandomTests(sequence);
const res = {
bits: sequence.length,
monobit: randomness.monobitTest(sequence),
frequency: analyzeTest(r.frequencyTest()),
longestRunOfOnes: analyzeTest(r.longestRunOfOnesTest()),
};
console.log(res);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment