Generate Random Whole Numbers with JavaScript Function

generate a random whole number function with test buttons

Demo

function randomWholeNum(max) {
  max = max + 1;
  var result_html = document.getElementById("result");
  var random_number = Math.floor(Math.random() * max);
  result_html.innerHTML = random_number;
  return random_number;
}

HTML

<div id='result' class='mb-3 alert alert-primary'>..</div>
<button onclick='randomWholeNum(20);' class='btn btn-primary mb-1'>Generate a Random Number Between 0 - 20</button> <button onclick='randomWholeNum(200);' class='btn btn-primary mb-1'>Generate a Random Number Between 0 - 200</button> <button onclick='randomWholeNum(2000);' class='btn btn-primary mb-1'>Generate a Random Number Between 0 - 2000</button>

Result

..

Other Items in js
Page Views

This page has been viewed 67 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹