Random Distribution List

atesh

Freshman
Joined
Aug 2, 2003
Messages
41
Location
Washington State, USA
I'm trying to generate 1,000 random whole numbers from 1 to 100, then put them in a listbox and show how many times each number(1 to 100) was picked randomly.
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rambo As Integer
        Dim x, y As Integer
        Dim dist(100) As Integer
        For x = 1 To 1000
            rambo = New System.Random().Next(1, 100)
            dist(rambo) += 1
        Next

        For y = 1 To 100
            ListBox1.Items.Add(y & "-- " & dist(y))
        Next

    End Sub

But when using the code above I get all numbers 1 to 100 as -- 0, except the following: 3-- 160, 25-- 241, 48-- 209, 58-- 148, 80-- 242.

So what the heck is going on?
Thanks alot.
 
What line and what is the code you are using with what I showed you? If the rest is the same and you just added the declaration and used that object it should work.
Here is what works:
Visual Basic:
        Dim rambo As Integer
        Dim x, y As Integer
        Dim dist(100) As Integer
        Dim r As New Random
        For x = 1 To 1000
            rambo = r.Next(1, 100)
            dist(rambo) += 1
        Next
        For y = 1 To 100
            ListBox1.Items.Add(y & "-- " & dist(y))
        Next
 
No, the error means that you dont have an instance of the Random object.
Are you sure you have the New keyword when declaring your Random object?
 
Back
Top