jQuery Generate Random Numbers Between Two Values

I discuss how to generate random numbers within a range with jQuery in my previous post. Actually there are many custom jQuery functions on the Internet that can generate random numbers between two values. These jQuery functions save you a lot of time.

The jQuery function that I have been using for many years to generate random numbers between two values is called JQuery Random Plugin written by Christian Bruun. This is a very small and handy jQuery function with only 6 lines of code.

Let’s see how to use these jQuery function to generate random numbers between two values.

The first step is download and save the file (jquery.random.js)  to your development server or hosting server.

Same as before, you have to load the jquery.js library and the function when the web page open. You can do that by adding the following two lines inside the header section of web pages:

<script type=”text/javascript” src=”jquery.js”></script>
<script type=”text/javascript” src=”jquery.random.js”></script>

Now, you are ready to use the jquery.random.js plugins. The following is a simple example showing how to generate random numbers between two values with the jquery.random.js plugins or function.

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery.random.js"></script>    

    <script type="text/javascript">

        jQuery("document").ready(function() {

        function loopRandomNumberBetween() {

            // How to generate a random number between 2 values:
            // jQuery.randomBetween(min, max);
            // e.g. jQuery.randomBetween(12, 18);

            // Display random numbers on text field
            jQuery(".outputText").text(jQuery.randomBetween(12, 18));

        // Execute the function in set interval (milli-seconds)
        setInterval(loopRandomNumberBetween, 1000);

    <h1>jQuery Random Numbers Between Two Values</h1>
    The random number is: <span>Random Number...</span>


jQuery Example File:

Click here to download.

As you can see, generate random numbers between two values with jQuery is easy.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>