JavaScript Notes
Learn to build a random number generator in JavaScript with min/max range, integer/decimal modes, history log, exclude list, and bulk generation.
What You'll Learn
- How
Math.random()works and its range[0, 1) - How to generate integers and decimals in a custom range
- How to exclude specific numbers from results
- How to generate bulk batches of random numbers
- How
crypto.getRandomValues()provides cryptographically secure randomness
Project Overview
This project goes beyond a single button — it lets users configure min/max, integer vs decimal mode, decimal precision, excluded values, and batch size. A history panel keeps track of every generated number.
Component Structure
How It Works — Flow Diagram
getRandomInt(min, max)
→ Math.floor(Math.random() * (max - min + 1)) + min
getRandomFloat(min, max, decimals)
→ (Math.random() * (max - min) + min).toFixed(decimals)
generate()
→ loop until a non-excluded number is found (with escape hatch)
→ push to history[]
→ renderStep-by-Step Build
Step 1 — HTML Structure
<div class="rng-app">
<h1>🎲 Random Number Generator</h1>
<div class="config-panel">
<div class="config-row">
<label>Min: <input type="number" id="minVal" value="1"></label>
<label>Max: <input type="number" id="maxVal" value="100"></label>
</div>
<div class="config-row">
<label><input type="radio" name="mode" value="integer" checked> Integer</label>
<label><input type="radio" name="mode" value="decimal"> Decimal</label>
<label id="decPrecisionLabel">Precision: <input type="number" id="precision" value="2" min="1" max="10"></label>
</div>
<div class="config-row">
<label>Exclude (comma-separated): <input type="text" id="excludeInput" placeholder="e.g. 13, 42"></label>
</div>
<div class="config-row">
<label>Batch size: <input type="number" id="batchSize" value="10" min="1" max="1000"></label>
</div>
</div>
<div id="resultDisplay" class="result-display">
<span id="resultNumber">—</span>
</div>
<div class="btn-row">
<button id="generateBtn" class="btn-gen">🎲 Generate</button>
<button id="batchBtn" class="btn-batch">📋 Batch</button>
<button id="clearBtn" class="btn-clear">🗑 Clear</button>
</div>
<div class="history-section">
<h3>History (<span id="histCount">0</span>)</h3>
<div id="historyDisplay" class="history-display"></div>
</div>
<div id="batchOutput" class="batch-output hidden"></div>
</div>Step 2 — Core Random Functions
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomFloat(min, max, decimals) {
const raw = Math.random() * (max - min) + min;
return parseFloat(raw.toFixed(decimals));
}Step 3 — Generate with Exclusion
Step 4 — Batch Generation
Step 5 — Render History
Step 6 — Clear History
Step 7 — Generate Button & Keyboard
Step 8 — Crypto-Secure Alternative (Bonus)
Console Output
getRandomInt(1, 100) → 42 getRandomInt(1, 100) → 7 getRandomInt(1, 100) → 99 // With exclusions: [13, 42] generate() → tries 42 → excluded → tries again → 17 ✅ // Decimal mode, precision=2, min=0, max=1 getRandomFloat(0, 1, 2) → 0.73 getRandomFloat(0, 1, 2) → 0.09 // Batch of 5, integer, 1-6 (dice) [3, 6, 1, 4, 2]
Edge Cases to Handle
| Situation | Expected Behavior |
|---|---|
| min >= max | Alert and block generation |
| All values excluded | Error after 1000 attempts |
| Decimal mode, precision 0 | Same as integer mode |
| Batch size 1000 | Works, but may be slow |
| Space bar on input | Don't trigger generate |
Challenge Yourself
- Dice roller — preset buttons for D4, D6, D8, D10, D12, D20
- Lottery picker — generate 6 unique numbers from 1–49 (no repeats)
- Normal distribution — generate numbers following a bell curve
- Random color — generate a random hex color code and show a preview
- Statistics — after 100 generates, show mean, min, max, and distribution histogram
Best Practices
- Use
Math.ceil(min)andMath.floor(max)ingetRandomIntto handle float inputs correctly - The
do/whileexclusion loop needs an escape hatch (max attempts) Math.random()is not cryptographically secure — usecrypto.getRandomValues()for security-sensitive cases- Keep generated number history in an array, not the DOM
toFixed()returns a string — useparseFloat()to convert back to a number
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a JavaScript Random Number Generator - Step by Step Tutorial.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, random
Related JavaScript Master Course Topics