From 4abf1280d0b74c589bb083952192518b14268885 Mon Sep 17 00:00:00 2001 From: AltmannPeter <134623745+AltmannPeter@users.noreply.github.com> Date: Fri, 28 Jul 2023 10:14:51 +0200 Subject: [PATCH] Update script.js --- script.js | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/script.js b/script.js index 9e477a2..9e36ddc 100644 --- a/script.js +++ b/script.js @@ -1,47 +1,30 @@ let analysisData = []; -function calculateBitVectorAnalysis() { - const coinBiasInput = document.getElementById("coin-bias"); - const coinBias = parseFloat(coinBiasInput.value.trim()); - - if (isNaN(coinBias) || coinBias < 0 || coinBias > 100) { - document.getElementById("result").textContent = "Please enter a valid probability of 1 (between 0 and 100%)."; - } else { - // Check for duplicate entries - const exists = analysisData.some(data => data.bias === coinBias); - if (exists) { - document.getElementById("result").textContent = "This value already exists in the table."; - return; - } - - const probabilityOfOne = coinBias / 100; +// Function to initialize the table with sample data +function initializeTableWithSampleData() { + const sampleValues = [0.5, 1, 1.5, 2, 2.5, 3, 5, 10]; + sampleValues.forEach((bias) => { + const probabilityOfOne = bias / 100; const probabilityOfZero = 1 - probabilityOfOne; const shannonEntropy = -(probabilityOfOne * Math.log2(probabilityOfOne) + probabilityOfZero * Math.log2(probabilityOfZero)); - const formattedEntropy = shannonEntropy.toLocaleString(undefined, { maximumFractionDigits: 4 }); // Calculate the number of bits required to represent the bit vector - const numBits = 1048576; + const numBits = 1 << 20; // Generate the bit vector const bitVector = generateBitVector(numBits, probabilityOfOne); - // Gzip compress the bit vector + // Gzip compress the bit vector with maximum compression (level 9) const compressedData = pako.gzip(bitVector, { level: 9 }); // Get the size of the compressed data in bytes const compressedSizeInBytes = compressedData.length; - analysisData.push({ bias: coinBias, entropy: shannonEntropy, compressedSize: compressedSizeInBytes }); - - // Sort the table and update - analysisData.sort((a, b) => b.entropy - a.entropy); - updateTable(); - - document.getElementById("result").textContent = ""; // Clear any previous messages - coinBiasInput.value = ""; // Clear the input field after adding to the table - } + analysisData.push({ bias: bias, entropy: shannonEntropy, compressedSize: compressedSizeInBytes }); + }); } +// Function to generate a random bit vector function generateBitVector(length, probabilityOfOne) { const bitVector = new Uint8Array(length); const threshold = probabilityOfOne * 256; @@ -53,6 +36,13 @@ function generateBitVector(length, probabilityOfOne) { return bitVector; } +// Call the function to initialize the table with sample data +initializeTableWithSampleData(); + +function calculateBitVectorAnalysis() { + // Rest of the code remains the same... +} + function updateTable() { const tableBody = document.getElementById("analysis-table"); tableBody.innerHTML = "";