Bitcoin Mining Simulation
Explore the fascinating world of cryptocurrency mining through interactive JavaScript examples. Simulate the process of Bitcoin mining and understand blockchain concepts.
Introduction to Bitcoin Mining
Bitcoin mining involves the process of producing a hash whose value is less than the target value. When this hash has been found, it is called a valid hash and hence proof of work is achieved.
The mining algorithm uses a counter known as the nonce to generate the hash using the SHA256 cryptographic function. A hash algorithm always produces the same arbitrary length data given the same inputs. It is impossible to compute the same hash with two different inputs. It is also impossible to predict the output of any given data in advance.
- Proof of Work: Miners compete to solve complex mathematical problems
- Nonce: A number used once that miners change to find valid hashes
- Target Difficulty: The threshold that a hash must be below to be valid
- Hash Rate: The speed at which a mining machine operates
- Block Reward: The incentive miners receive for finding a valid block
For simulation purposes, we'll use integer values instead of actual cryptographic hashes to demonstrate the concept in an accessible way. The process remains the same - finding a number below a certain target by trying billions or trillions of nonce values.
Bitcoin Mining Simulation
This simulation demonstrates how Bitcoin miners search for a valid hash by trying different nonce values. The goal is to find a random number below the target value of 12999.
// Bitcoin Mining Simulation const target = 12999; let nonce = 0; let miningInterval; function startMining() { miningInterval = setInterval(() => { // Generate a random number between 1 and 1000000 const randomNum = Math.floor(1 + Math.random() * 1000000); nonce++; // Update display document.getElementById('miningDisplay').textContent = randomNum; document.getElementById('nonceValue').textContent = nonce; // Check if we found a valid hash if (randomNum < target) { stopMining(); document.getElementById('miningStatus').textContent = 'Mining Successful! You found a valid hash.'; document.getElementById('miningStatus').style.color = 'var(--success)'; } }, 20); } function stopMining() { clearInterval(miningInterval); }
Bitcoin Mining Live Demo
Click the "Start Mining" button to begin searching for a valid hash. The simulation will run until it finds a number below 12999.
Nonce Value
0
Target
12999
Ready to start mining...
Digital Dice Example
A digital dice implementation that generates random numbers between 1 and 6. This example demonstrates the use of random number generation and DOM manipulation.
// Digital Dice Implementation const diceElement = document.getElementById('dice'); const rollBtn = document.getElementById('rollDice'); const diceValues = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅']; function rollDice() { rollBtn.disabled = true; diceElement.style.transition = 'transform 0.1s ease'; let rolls = 0; const rollInterval = setInterval(() => { const randomIndex = Math.floor(Math.random() * diceValues.length); diceElement.textContent = diceValues[randomIndex]; diceElement.style.transform = `rotate(${rolls * 30}deg) scale(${1 + rolls*0.05})`; rolls++; }, 100); setTimeout(() => { clearInterval(rollInterval); const result = Math.floor(Math.random() * 6) + 1; diceElement.textContent = diceValues[result - 1]; diceElement.style.transform = 'rotate(0deg) scale(1)'; diceElement.style.transition = 'transform 0.5s ease'; rollBtn.disabled = false; }, 1500); } rollBtn.addEventListener('click', rollDice);
Digital Dice Live Demo
Graphical Dice Example
A graphical dice implementation that visually represents each dice value. This example demonstrates DOM manipulation, CSS styling, and array handling.
// Graphical Dice Implementation const graphicalDice = document.getElementById('graphicalDice'); const rollGraphicalBtn = document.getElementById('rollGraphicalDice'); // Dice dot patterns for each value const dicePatterns = [ [], // 0 (not used) [5], // 1 - center [1, 9], // 2 - top-left, bottom-right [1, 5, 9], // 3 [1, 3, 7, 9], // 4 - corners [1, 3, 5, 7, 9], // 5 [1, 3, 4, 6, 7, 9] // 6 ]; function rollGraphicalDice() { rollGraphicalBtn.disabled = true; graphicalDice.style.transition = 'transform 0.1s ease'; let rolls = 0; const rollInterval = setInterval(() => { const tempValue = Math.floor(Math.random() * 6) + 1; drawDice(tempValue); graphicalDice.style.transform = `rotate(${rolls * 30}deg) scale(${1 + rolls*0.05})`; rolls++; }, 100); setTimeout(() => { clearInterval(rollInterval); const result = Math.floor(Math.random() * 6) + 1; drawDice(result); graphicalDice.style.transform = 'rotate(0deg) scale(1)'; graphicalDice.style.transition = 'transform 0.5s ease'; rollGraphicalBtn.disabled = false; }, 1500); } function drawDice(value) { graphicalDice.innerHTML = ''; const pattern = dicePatterns[value]; for (let i = 1; i <= 9; i++) { const dot = document.createElement('div'); dot.className = 'dice-dot'; dot.style.opacity = pattern.includes(i) ? '1' : '0'; graphicalDice.appendChild(dot); } } rollGraphicalBtn.addEventListener('click', rollGraphicalDice); drawDice(1);
Graphical Dice Live Demo
More Examples
Explore more JavaScript examples to enhance your learning experience: