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 double type pseudo random numbers and populating byte array with random numbers VB and C# using .net 2.0.

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

In this simple article you will learn that how we can generate double type (non negative) pseudo random numbers and populating byte array with random numbers using "Random class" in VB and C# (.net 2.

 Generate Double Random Numbers:

To generate random numbers of double type we have a function named NextDouble (). This function takes no parameter and returns a double number. This function generates random number of type double between 0.0 to 1.0.

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

Now write the following code on Button click event:

C#

private void btn_doubleRandom_Click(object sender, EventArgs e)

        {

            double number = 0.0;

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

            {

                string str = "NUMBER IS: ";

                number = obj.NextDouble();

                str=str+number.ToString(0);

                MessageBox.Show(str);

            }

        }

VB

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

        Dim number As Double = 0.0R

        For i As Integer = 0 To 4

            Dim str As String = "NUMBER IS: "

            number = obj.NextDouble()

            str = str + number.ToString(0)

            MessageBox.Show(str)

        Next

    End Sub

This simple code generates five random numbers of double type.

Populating Byte Array:

Random class also provides us a facility to populate a  byte array with random numbers. For this purpose we have a function named NextBytes(). This function takes byte array and fills that array with random number. These numbers are between 0 to 255 which is the range of a byte.

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

Now write the following code on Button click event:

C#

private void btn_ByteArrray_Click(object sender, EventArgs e)

        {

            byte[] arr = new byte[10];

            obj.NextBytes(arr);

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

            {

                string str;

                str = arr[i].ToString();

              MessageBox.Show("NUMBER ON INDEX " + i.ToString() + " " + str);

            }

        }

VB

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

        Dim arr As Byte() = New Byte(9) {}

        obj.NextBytes(arr)

        For i As Integer = 0 To 9

            Dim str As String

            str = arr(i).ToString()

            MessageBox.Show(("NUMBER ON INDEX " & i.ToString() & " ") + str)

        Next

    End Sub

This simple code generates ten random numbers and fills the byte array with those numbers.

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 double type (non negative) pseudo random numbers and populating byte array with random numbers using "Random class" in VB and C# (.net 2.0).

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Generate integer type pseudo random numbers using Random class VB and C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net