Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Generate integer type pseudo random numbers using Random class VB and C#.

Author: Maliha Atteeq
Download Source Code : 965_random.zip

In this simple article you will learn that how we can generate integer type (non negative) pseudo random numbers using "Random class" in VB and C# (.net 2.0).

 Random Numbers:

Sequence of numbers that meet certain statistical requirements of randomness. These are pseudo random numbers which are generated through an algorithm. .net provides a class named Random this class generate random numbers. We can use its functions to generate random numbers as discussed below.

Generate Random Numbers:

To generate random numbers using random class we have a function named next this function returns non negative random numbers. There are three overloaded method of this function. These three methods generate random numbers but these methods take different parameters and generate numbers according to the parameters

First Method:

First overloaded method is simplest method it does not take any parameter and returns non negative integer. Means its range is zero to positive infinity.

To demonstrate make a window application. Drag one button on form.

Now write the following code on Button click event:

C#

        private void btn_simple_Click(object sender, EventArgs e)

        {

            int number=0;

            for (int i = 0; i < 5; i++)

            {

                number = obj.Next();

                string str = "NUMBER IS ";

                str = str + number.ToString();

                MessageBox.Show(str);

            }

VB

Private Sub btn_simple_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim number As Integer = 0

        For i As Integer = 0 To 4

            number = obj.[Next]()

            Dim str As String = "NUMBER IS "

            str = str + number.ToString()

            MessageBox.Show(str)

        Next

    End Sub

This simple code generates five random numbers.

Second Method:

This overloaded method takes an integer which defines the upper bound of the random numbers and that number is exclusive. Means the range of the number will be greater then equal to zero and less then given range. The maximum value for range should be greater or equal to zero other wise there will be error.

To demonstrate make a window application. Drag one text box and one button on form.

Now write the following code on Button click event:

C#

    private void btn_MaxRange_Click(object sender, EventArgs e)

        {

            int number = 0;

            string rng = txt_MaxRange.Text;

            int max = Convert.ToInt32(rng);

            if (max >= 0)

            {

                for (int i = 0; i < 5; i++)

                {

                    number = obj.Next(max);

                    string str = "NUMBER IS ";

                    str = str + number.ToString();

                    MessageBox.Show(str);

                }

            }

            else

                MessageBox.Show("invalid range");

        }

VB

Private Sub btn_MaxRange_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim number As Integer = 0

        Dim rng As String = txt_MaxRange.Text

        Dim max As Integer = Convert.ToInt32(rng)

        If max >= 0 Then

            For i As Integer = 0 To 4

                number = obj.[Next](max)

                Dim str As String = "NUMBER IS "

                str = str + number.ToString()

                MessageBox.Show(str)

            Next

        Else

            MessageBox.Show("invalid range")

        End If

    End Sub

This simple code generates five random numbers according to the given range.

Third Method:

This overloaded method takes two integers which define the lower and upper bound of the random numbers upper bound number is exclusive and lower bound number is inclusive in generated random numbers. Means the range of the number will be greater then equal to lower bound number and less then given upper bound range. The maximum and minimum value for range should be greater or equal to zero other wise there will be error.

To demonstrate make a window application. Drag two text boxes and one button on form.

Now write the following code on Button click event:

C#

private void btn_MinMax_Click(object sender, EventArgs e)

        {

            int number = 0;

            string Maxrng = txt_MaxRang.Text;

            string Minrng = txt_MinRange.Text;

            int max = Convert.ToInt32(Maxrng);

            int min = Convert.ToInt32(Minrng);

            if (max >= 0&&min>=0)

            {

                for (int i = 0; i < 5; i++)

                {

                    number = obj.Next(min,max);

                    string str = "NUMBER IS ";

                    str = str + number.ToString();

                    MessageBox.Show(str);

                }

            }

            else

                MessageBox.Show("invalid range");

        }

VB

Private Sub btn_MinMax_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim number As Integer = 0

        Dim Maxrng As String = txt_MaxRang.Text

        Dim Minrng As String = txt_MinRange.Text

        Dim max As Integer = Convert.ToInt32(Maxrng)

        Dim min As Integer = Convert.ToInt32(Minrng)

        If max >= 0 AndAlso min >= 0 Then

            For i As Integer = 0 To 4

                number = obj.[Next](min, max)

                Dim str As String = "NUMBER IS "

                str = str + number.ToString()

                MessageBox.Show(str)

            Next

        Else

            MessageBox.Show("invalid range")

        End If

    End Sub

This simple code generates five random numbers according to the given range.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP RANDOM NUMBERS APPLICATION";          

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP RANDOM NUMBERS APPLICATION"

    End Sub

This simple article tells that how we can generate integer type (non negative) pseudo random numbers using "Random class" in VB and C# (.net 2.0).

Article Comments
This helps me totaly as i want

Posted on 11/10/2010 11:07:44 PM by saqib khalil

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How we can check that sorted list has fixed size or not and it is synchronized or not and also trim the list to actual size only using “sorted list” class in vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net