By Team Escalera
on 31 Oct 2017 03:20 PM
  • learn C#

It is sometimes needed to generate random integers in C# programming.

Random integers can be used  for human verification in Math Captcha .

Random integers can be used for sms verification etc.

Let’s see how to do it.

Our Aspx Code

<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” Runat=”Server”>

    <asp:Label ID=”lblRandom” runat=”server” Text=”Label”></asp:Label>

    <asp:Label ID=”lblRandom2″ runat=”server” Text=”Label”></asp:Label>

    <asp:Label ID=”lblSum” runat=”server” Text=”Label”></asp:Label>


lblRandom for generation of 1st Random Character

lblRandom2 for generation of 2nd Random Character

lblSum to generate Sum of 2 random numbers

Now C# Code –
protected void Page_Load(object sender, EventArgs e)
        Random rnd = new Random();
        int month = rnd.Next(1, 13);
        Random rnd1 = new Random();
        int month12 = rnd1.Next(13, 23);
        lblRandom.Text = month.ToString();
        lblRandom2.Text = month12.ToString();

“Random” OR Class System.Random => Represents a Pseudo-Random number Generator

rnd.Next (Return a value in specified integer range )

Now we can add these values , compare them with user input or do whatever in needed in the application.

We are simply printing the random numbers in our two Labels.

Comments (0)

Leave A Comment