Jquery Script to Generate Random Number in Asp.Net C# VB.Net Aspx Page

In many forums i found a question “How to generate a Random Number using Jquery?”. Its easy to generate a Random number because Jquery provides us builtin Math library function named Math.random() to generate Random Numbers as well as lot of other math functionalities. To get the output you have to put a number range. Then use the mehod Math.random() to produce Random Numbers. You can also use this technique for dice, random image script, or random link generator.

Click here to get Javascript Random Number Generation Script.

Random Number Generation:

To do the JQuery Example please follw the below steps:
1. Add Jquery mini script first

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

2. Add a Server Side Button Control

<asp:Button ID="Button1" runat="server" Text="Generate Random Number" />

3. Add a Viv

<div id="divNumber"></div>

4. Add the Random Number Generation Script

    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%= Button1.ClientID %>').click(function () {
                var Random_Number = Math.ceil(Math.random() * 500); // Generate random number between 1 and 500               
                $("#divNumber").append("<b>The Number is: </b>" + Random_Number + "</br>");
                return false;

5. Now run the project.

Note: Full Code Example is same for both C# & VB.Net

Download Code Example C#        Download Code Example VB.Net

2 thoughts on “Jquery Script to Generate Random Number in Asp.Net C# VB.Net Aspx Page

  • March 24, 2015 at 12:32 pm

    Very informative post for me as I am always looking for new content that can help me and my knowledge grow better.

  • June 9, 2017 at 7:56 am

    I\’m new to learning JS/jQuery. I looked everywhere trying to figure out how to create a random number using just jQuery. With a few minor modifications I was finally able to do so using your exceptional example. Thank you so much for making the solution so clear to a beginner like me!


Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code