Random number in Javascript every X seconds

Posted on 2009-02-10
Last Modified: 2012-05-06
How can I use javascript to generate a random number between X and Y every Z seconds?

I am wanting to make a sort of fake page counter so once the page loads, I am wanting it to say something like "Number of people currently viewing: XXXX"  where XXXX changes every second or so.

Thanks in advance!
Question by:plasticbrain
    LVL 15

    Accepted Solution

    This should do the trick.  Credit to the following for saving me some brain time in figuring ranges:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
    <html xmlns="" lang="en" xml:lang="en">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <script type="text/javascript">
      var timer = null;
      function startcounter(low, high, time, targetid){
          generate(low, high, targetid);
          time = time * 1000;
          timer = setInterval("generate(" + low + ", " + high + ", '" + targetid + "')", time);
      function generate(low, high, targetid){
          var target = document.getElementById(targetid);
          var counter = null;
          counter = Math.floor((high - (low - 1)) * Math.random()) + low;
          target.innerHTML = counter;
    <body onload="startcounter(100, 110, 1, 'rnum');">
     <div id="rnum">&nbsp;</div>

    Open in new window


    Author Closing Comment

    This is exactly what I was looking for! Thank you for the excellent code snippet.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article shows how to read a single webpage's HTML into a string variable, and it also shows how to automate a sequence so you can read and process a list of webpages.  I was tasked with reading and parsing a series of webpages to collect some s…
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now