JavaScript Application For Random Number Generator (original) (raw)

Last Updated : 18 Mar, 2026

A random number generator is a simple yet practical JavaScript project that introduces core programming concepts. It helps you understand user input handling, random value generation, and dynamic UI updates.

Approach

We’ll build a user-friendly application that allows users to:

The application will have a clean interface and intuitive controls, making it easy for anyone to use.

Project Preview

djnd

JavaScript Application For Random Number Generator

Random Number Generator - HTML and CSS

The HTML code creates the structure for a Random Number Generator web application, including input fields for the minimum and maximum range, a button to generate the number, and a button to reset the inputs. It also displays the generated random number dynamically on the page and the CSS Styles the generate button, reset button and the result displaying area.

HTML `

Random Number Generator

Minimum: Maximum:
Generate Random Number

Click the button to generate

Reset

`

Random Number Generator - JavaScript Logic

The JavaScript code powers the functionality of the Random Number Generator by handling user input, generating random numbers within the specified range, and updating the result display. It also provides a reset feature to clear inputs and the displayed number.

JavaScript `

const min = document.getElementById('min'); const max = document.getElementById('max'); const genBtn = document.getElementById('generate'); const resetBtn = document.getElementById('reset'); const randNumDisplay = document.getElementById('randomNumber');

function genRandNum() { const minVal = parseInt(min.value); const maxVal = parseInt(max.value);

if (isNaN(minVal) || isNaN(maxVal) || minVal >= maxVal) {
    alert("Please enter valid minimum and maximum values.");
    return;
}

const randNum = Math.floor(Math.random() * (maxVal - minVal + 1)) + minVal;
randNumDisplay.textContent = randNum;

}

function reset() { min.value = 1; max.value = 100; randNumDisplay.textContent = "Click the button to generate"; }

genBtn.addEventListener('click', genRandNum); resetBtn.addEventListener('click', reset);

`

Complete code

HTML `