Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
           CodeHim.com

Free Web Design Code & Scripts

       
  • Home
  • Code Snippets
    • Accordion
    • Creative
    • Carousel
    • Date & Time
    • Gallery
    • LightBox
    • Menu & Nav
    • Modal
    • Others
    • Portfolio
    • Social Media
    • Text & Input
    • Video Player
    • Zoom
  • HTML & CSS
  • Bootstrap
  • JavaScript
  • Collections
  • About
  • Contact
  • Buy Me a Coffee

Home / Vanilla JavaScript / JavaScript Random Number Generator Between Range

JavaScript Random Number Generator Between Range

January 22, 2024January 10, 2024 by Asif Mughal
JavaScript Random Number Generator Between Range
Share This:
Code Snippet:random number generator
Author: Vincent Salazar
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 2,018
License: MIT
Edit Code online: View on CodePen
Read More
 Demo
Download (File path is not allowed!)

This JavaScript code snippet helps you to create a random number generator tool between range. It uses the Math.random() function to generate a random number. It comes with a basic interface where the user can enter a range, and the code will generate a random number within that range.

The code also displays the random number in a list, and the list items are styled with different colors. You can use this code for random passwords or other security codes.

How to Create JavaScript Random Number Generator

1. First, create the HTML structure for the random number generator as follows:

<div class="shader"></div>
<h2>Random Number Generator</h2>
    <h3>between 1 and <span id="placeholder">10</span></h3>
    <div class="wrapper">
        <div id="r-container">
            <span id="rand"></span>
        </div>
        <div class="i-container">
            <input onChange="onChange()" type="number" value="10" />
            <button id="button" onclick="random()">Generate</button>
            <div class="reset"><span onclick="resetLi()">Reset</span></div>
        </div>
        <div class="l-container">
            <ul id="list">

            </ul>
        </div>
</div>

2. Add the following CSS code to your project’s stylesheet or in a <style> tag within the HTML. This ensures the visual styling and responsiveness of the random number generator. Modify the CSS styles to match your project’s overall theme. You can customize colors, fonts, or any other visual elements to seamlessly integrate with your design.

* {
  box-sizing: border-box;
}

body{
  font-family: sans-serif;
  background: url("https://picsum.photos/1000/1000");
  background: linear-gradient(to right, #000000, #434343) !important;
  background-attachment: fixed;
  background-position: center;
  background-repeat: none;
  background-size: cover;
  transition: boxShadow 3s;
  margin: 0;
  padding: 0;
  color: white;
  background-color: black;
  min-height: 100vh;
}

 .shader {
  background-color: #000000bb;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
}
 h2 {
  text-align: center;
  margin-top: 0;
  padding-top: 1rem;
}
 h3 {
  text-align: center;
}
 #r-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 10vh;
  margin: 3rem 0;
}
 #r-container span {
  text-align: center;
  font-size: 5rem;
}
 .i-container {
  min-height: 2rem;
  display: flex;
  justify-content: center;
  align-items: center;
}
.i-container input {
  outline: none;
  border: none;
  color: white;
  background-color: transparent;
  border-bottom: 1px white solid;
  text-align: center;
  width: 7rem;
  font-size: 3vw;
}
@media (min-width: 769px) {
 .i-container input {
    font-size: 2vw;
  }
}
 .i-container button {
  background: transparent;
  font-size: 3vw;
  color: white;
  outline: none;
  border: none;
  margin: 0 0.5rem;
  padding: 1% 2%;
  cursor: pointer;
  transition: transform 0.15s;
  font-size: 3vw;
}
 .i-container button:hover {
  transform: translateY(-3.5px);
}
@media (min-width: 769px) {
 .i-container button {
    font-size: 2vw;
  }
}
 .i-container .reset {
  text-align: center;
  font-size: 3vw;
  transition: transform 0.15s;
  font-size: 3vw;
}
 .i-container .reset:hover {
  transform: translateY(-3.5px);
}
@media (min-width: 769px) {
 .i-container .reset {
    font-size: 2vw;
  }
}
 .i-container .reset span {
  cursor: pointer;
}
@media (min-width: 769px) {
 .l-container {
    padding: 0 5%;
  }
}
 .l-container #list {
  display: flex;
  flex-wrap: wrap;
}
 .l-container #list li {
  margin: 0.5rem 1rem;
  font-size: 3vw;
}
@media (min-width: 769px) {
.l-container #list li {
    font-size: 2vw;
  }
}

3. Finally, place the following JavaScript code at the end of your HTML file, just before the closing </body> tag. This ensures that the DOM is fully loaded before the script runs.

var input = document.querySelector("input");
var number = input.value;
var placeholder = document.getElementById("placeholder");
var list = document.getElementById("list");
var rand = document.querySelector("#rand");
var reset = document.querySelector(".reset");
var button = document.getElementById("button");
var body = document.querySelector("body");
// var colors = ["lightblue", "aquamarine", "pink", "bisque", "white"];
var colors = ['#FF6633', '#FFB399', '#FF33FF', '#FFFF99',
              '#00B3E6',
		  '#E6B333', '#3366E6', '#999966', '#99FF99', '#B34D4D',
		  '#80B300', '#809900', '#E6B3B3', '#6680B3', '#66991A', 
		  '#FF99E6', '#CCFF1A', '#FF1A66', '#E6331A', '#33FFCC',
		  '#66994D', '#B366CC', '#4D8000', '#B33300', '#CC80CC', 
		  '#66664D', '#991AFF', '#E666FF', '#4DB3FF', '#1AB399',
		  '#E666B3', '#33991A', '#CC9999', '#B3B31A', '#00E680', 
		  '#4D8066', '#809980', '#E6FF80', '#1AFF33', '#999933',
		  '#FF3380', '#CCCC00', '#66E64D', '#4D80CC', '#9900B3', 
		  '#E64D66', '#4DB380', '#FF4D4D', '#99E6E6', '#6666FF'];

function onChange() {
  number = input.value;
  placeholder.innerHTML = number
}

function random() {
  rand.innerHTML = Math.ceil(Math.random() * number)
  var text = document.createTextNode(rand.innerHTML)
  var node = document.createElement("LI");
  node.appendChild(text);
  console.log(node)
  var randNumber = Math.floor(Math.random() * colors.length);
  var randNum = Math.floor(Math.random() * colors.length);
  node.style.color = colors[randNumber];
  reset.style.color = colors[randNum];
  button.style.border = "solid 1px" + " " + colors[randNumber];
  body.style.boxShadow = "inset 0 0 500px -200px" + " " + colors[randNumber]
  list.appendChild(node)
}

function resetLi() {
  document.querySelectorAll("li").forEach((e, i, _p) => {
    e.remove();
  })
  document.querySelector("#rand").innerHTML = ""
}

That’s all! hopefully, you have successfully created a JavaScript random number generator between range on your website. If you have any questions or suggestions, feel free to comment below.

Similar Code Snippets:

  • Before After Image Slider in Vanilla JavaScript

    Before After Image Slider in Vanilla JavaScript

  • Fahrenheit to Celsius Converter JavaScript Code

    Fahrenheit to Celsius Converter JavaScript Code

  • JavaScript Move Focus with Arrow Keys

    JavaScript Move Focus with Arrow Keys

  • JavaScript Change CSS Variable Root

    JavaScript Change CSS Variable Root

  • Toggle Switch with Accessibility in JavaScript

    Toggle Switch with Accessibility in JavaScript

  • JavaScript Digital Clock with Date

    JavaScript Digital Clock with Date

  • JavaScript Get Client System Information

    JavaScript Get Client System Information

  • JavaScript Autocomplete Dropdown

    19+ JavaScript Autocomplete Dropdown Sample & Tutorial

  • Product Recommendation Based On Selection in JavaScript

    Product Recommendation Based On Selection in JavaScript

Asif Mughal

I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.

Categories Vanilla JavaScript
CSS Text Color Change Animation
Testimonial Quotes Cloud in HTML CSS

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Categories

    • Accordion 22
    • Bootstrap 81
      • Bootstrap Accordion 1
      • Bootstrap Buttons 4
      • Bootstrap Cards 2
      • Bootstrap Carousel 2
      • Bootstrap Dropdowns 2
      • Bootstrap Forms 6
      • Bootstrap Jumbotron 1
      • Bootstrap Modals 2
      • Bootstrap Navbars 6
      • Bootstrap Panels 1
      • Bootstrap Progress Bars 1
      • Bootstrap Text & Input 3
    • Carousel 58
    • Chart & Graph 15
    • Date & Time 49
    • Gallery 39
    • HTML5 & CSS3 201
    • Layout 17
    • LightBox 19
    • Menu & Nav 95
    • Modal 18
    • Others 58
    • Portfolio 8
    • Social Media 10
    • Text & Input 120
    • Vanilla JavaScript 163
    • Zoom 12

You May Like

Alert (7) Analog Clock (4) Analog Clocks (5) Back to Top (4) Calculator (16) Calendar (5) Chart (3) Color Picker (4) Contact Us Forms (5) Countdown Timer (6) Datepicker (9) Digital Clocks (10) Drag and Drop (4) Dropdown Menu (23) File Uploader (4) Hamburger Menu (9) Horizontal Menu (24) Hover Effects (5) HTML5 Canvas (8) Image Sliders (40) Mega Menu (11) Modal (5) Multi-Level Menu (9) News Ticker (4) Notification (9) Off-Canvas Menu (10) Pagination (5) Parallax (8) Popup Lightbox (20) Progress Bar (5) Range Sliders (4) Scrolling Effects (28) Search Box (4) Select Dropdown (4) Shopping Cart (3) Side Menu (16) Slideshow (19) Social Share Buttons (3) Tabs (9) Text (11) Timeline (5) Timer (4) Tooltip (4) Typing Effect (6) Vertical Menu (14)

Latest Code Snippets

  • Common Open Source Security Vulnerabilities and How to Mitigate ThemCommon Open Source Security Vulnerabilities and How to Mitigate Them
    October 12, 2024
  • Animating Grid-column on Hover Using CSSAnimating Grid-column on Hover Using CSS
    April 1, 2024
  • Star Trails Animation On Mousemove in JavaScriptStar Trails Animation On Mousemove in JavaScript
    March 31, 2024
  • Gravity Button Using Pure CSSGravity Button Using Pure CSS
    March 31, 2024
  • Padlock Toggle Switch in CSSPadlock Toggle Switch in CSS
    March 31, 2024
  • Scroll-driven Animations in Pure CSSScroll-driven Animations in Pure CSS
    March 31, 2024
  • Interactive Notification Center UI in Vanilla JSInteractive Notification Center UI in Vanilla JS
    March 31, 2024
  • Emoji Spinner in Vanilla JavaScriptEmoji Spinner in Vanilla JavaScript
    March 31, 2024
  • Reveal Secret Code Using CSSReveal Secret Code Using CSS
    March 31, 2024
  • Toggle Button in CSS With Stretchable Elastic EffectToggle Button in CSS With Stretchable Elastic Effect
    March 31, 2024

Popular

  • 65+ Login Page in HTML with CSS Code Sample Simple to Difficult - 947,396 views
  • 25+ Best JavaScript Shopping Cart Examples with Demo - 211,933 views
  • Bootstrap Multiselect Dropdown with Checkboxes - 150,948 views
  • Bootstrap 5 Buttons with Icon and Text Tutorial & Demo - 112,357 views
  • 19+ Bootstrap Select Dropdown with Search Box Tutorial & Examples - 101,034 views
  • Bootstrap 5 Sidebar Menu with Submenu Collapse/Hover Tutorial Demo - 88,103 views
  • 99+ Social Media Buttons HTML Code Sample & Tutorial - 86,392 views
  • 19+ Bootstrap 5 Mega Menu Responsive/Drop Down Examples - 86,388 views
  • Bootstrap 4 Modal Popup Login Form Tutorial & Demo - 82,145 views
  • Bootstrap Vertical Menu with Submenu on Click - 80,199 views

Advertisement

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

CodeHim
  • About
  • Contact Us
  • Terms and Conditions
  • Privacy Policy
Top Categories
  • Bootstrap Snippets
  • JavaScript Snippets
  • HTML CSS Snippets
  • Menu & Nav Snippets
Get in Touch

Follow us on social media to be updated with latest web design code & scripts...

All Rights Reserved © 2026 - CodeHim Inc

Please Rel0ad/PressF5 this page if you can't click the download/preview link

F5
X