I'm new to VB, and I'm having some trouble creating an array of random numbers between 100 and 150 (where the numbers represent # of students). I'm not sure what I'm doing wrong, but my textbox is showing 0, no matter how many times I press the generate button.

here's my code:

Option Strict On

Public Class StudentGradeForm

    Dim Trials(151) As Integer, Grades(100) As Integer, i As Integer

    Private Sub GenerateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateButton.Click


        For i As Integer = 100 To 150
            ReDim Trials(i)
        Next
        For i As Integer = 50 To 100 : Grades(i) = i : Next
        Randomize()

        NumberOfTrialsText.Text = Trials(i).ToString("D")

    End Sub
End Class

Recommended Answers

All 3 Replies

If you want to store 50 numbers then the array declaration should be,

Dim Trials(49) As Integer

Where lower bound is 0 and upper bound (limit) is 49.

To generate random number, use Random class.

Dim rnd As New Random
 Randomize()
 no = rnd.Next(100, 150)

Array containing int elements between 100 to 150 in random order.

Dim rnd As New Random
        Dim Trials(49) As Integer
        Dim i As Integer

        For i = 0 To Trials.GetUpperBound(0)
            Trials(i) = 100 + i
        Next


        'Exchange elements between two random indexes
        Dim ind1, ind2, temp As Integer
        For i = 0 To 50
            ind1 = rnd.Next(0, 49)
            ind2 = rnd.Next(0, 49)

            temp = Trials(ind1)
            Trials(ind1) = Trials(ind2)
            Trials(ind2) = temp
        Next

How would you then print it out to, say, a listbox where it would provide the array location?
i.e:

1 -  101
2 -  112
3 -  142
4 -  102
etc.....

I know in C++ you could do a loop to ouprint the values, would you do the same for VB in this case?

something like this?

for (i=0;i<50;i++)
{
   for (j=1;j<=i;j++)
 cout << Array[i] << " - " << Array[j] << endl;
}

So I see what you are saying, but I think we have the numbers are all mixed up....I am trying to make an array of a varying number between 100-150, which has accompanying varying grades between 50-100...I've been trying to fix it, but I can't figure it out

Dim rnd As New Random
        Dim Trials(149) As Integer
        Dim cnt As Integer
        Dim Title1 As String = " #    Grade"
        Dim Title2 As String = "_____________"
        Dim fmt As String = "{0,3}{1,6}"
        Dim i As Integer

        For i = 0 To Trials.GetUpperBound(0)
            Trials(i) = 0 + i
        Next


        'Exchange elements between two random indexes
        Dim ind1, ind2, temp As Integer
        For i = 50 To 100
            ind1 = rnd.Next(50, 100)
            ind2 = rnd.Next(50, 100)

            temp = Trials(ind1)
            Trials(ind1) = Trials(ind2)
            Trials(ind2) = temp
            cnt += 1
        Next

        NumberOfTrialsText.Text = Trials(i).ToString("D")

        TrialsListBox.Items.Add(Title1) : TrialsListBox.Items.Add(Title2)
        TrialsListBox.Items.Add(String.Format(fmt, i, Trials(i)))

this gives me:

#   Grade
101 101
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.