Random Number Between A Given Range using JavaScript

Random Number Between A Given Range using JavaScript

Random Number Between A Given Range using JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Random Number Between A Given Range which will generates a function which randomly generate two numbers within a defined range. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Random Number Between A Given Range Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Random Number Between A Given Range Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Random Number Between A Given Range Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Random Number Between A Given Range Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Random Number Between A Given Range

First we’ll start with creating the structure of the Random Number Between A Given Range project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Random Number Generator</title>
    <!-- Google Font -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="wrapper">
        <div class="input-wrapper">
          <label for="min">Min:</label>
          <input type="number" id="min" value="0" />
        </div>
        <div class="input-wrapper">
          <label for="max">Max:</label>
          <input type="number" id="max" value="10" />
        </div>
      </div>
      <button id="generate">Generate</button>
      <div id="result"></div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

50+ HTML, CSS & JavaScript Projects With Source Code

CSS Code for Random Number Between A Given Range

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Random Number Between A Given Range project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  height: 100vh;
  background: linear-gradient(#f0f1f4 50%, #f5a623 50%);
}
.container {
  background-color: #ffffff;
  width: 85vw;
  max-width: 34em;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  padding: 3em;
  border-radius: 0.5em;
  box-shadow: 0 1.2em 2.8em rgba(0, 41, 22, 0.2);
}
.wrapper {
  display: flex;
  justify-content: space-between;
  gap: 3em;
}
label {
  font-size: 1.2em;
  font-weight: 600;
  color: #242e4c;
}
input {
  display: block;
  width: 100%;
  font-size: 1.3em;
  border: none;
  color: #444b5f;
  border-bottom: 1.5px solid #242e4c;
  padding: 0.45em;
  margin-top: 0.45em;
  outline: none;
}
input:focus {
  border-color: #f5a623;
}
button#generate {
  display: block;
  width: 100%;
  font-size: 1.1em;
  margin: 2.7em 0 1.1em 0;
  background-color: #f5a623;
  padding: 0.8em 0;
  border: none;
  border-radius: 0.5em;
  cursor: pointer;
  color: #ffffff;
}
#result {
  text-align: center;
  font-size: 3.7em;
  font-size: 600;
  color: #242e4c;
}

JavaScript Code for Random Number Between A Given Range

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Random Number Between A Given Range using HTML, CSS & JavaScript (Source Code).

let generateBtn = document.getElementById("generate");

function randomNum() {
  let min = document.getElementById("min");
  let max = document.getElementById("max");
  let minValue = Number(min.value);
  let maxValue = Number(max.value);
  if (minValue > maxValue) {
    minValue = maxValue + minValue;
    maxValue = minValue - maxValue;
    minValue = minValue - maxValue;
    min.value = minValue;
    max.value = maxValue;
  }
  let num = Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue;
  document.getElementById("result").innerText = num;
}
window.addEventListener("load", randomNum());
generateBtn.addEventListener("click", randomNum);

Restaurant Website Using HTML and CSS

Final Output

We have Successfully created our Random Number Between A Given Range using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply