Random Numbers and arrays

thankins

Newcomer
Joined
Sep 24, 2003
Messages
24
Hello everyone, I am trying to teach myself VB and so far have been very successful, but I have ran into a problem. Here it is. I am trying to create a lottery application that has an array of 5 numbers and that will generate a set of five numbers in the range of 0 to 9 for each array. Then the user should enter 5 numbers from Input boxes and they should be stored in another arrary. A for should display how many of the numbers match.

The reason I cant figure it out is becasue the book I am using is missing a few pages and so I dont know really where to begin. I know that I have to have two arrays, but i dont really know where to go from there.
Any help would be greatly appreciated!!!!!!


Thank you
Travis
 
here is what I have so far.

Code:
    Dim Lotto(4) As Integer
    Dim UserNum(4) As String
    Dim count As Integer

    Sub GetNum()
        For count = 0 To 4
            UserNum(count) = Val(InputBox("Please enter a number." & _
            (count + 1).ToString))
        Next count
    End Sub


    Sub RndNum()
        Dim randomNum As Integer
        Dim count As Integer

        Randomize()

        For count = 0 To 4
            Lotto(count) = randomNum = 1 + Int(Rnd() * 9)

        Next count


    End Sub
 
This piece of code it's a routine that generates 5 numbers into a ArrayList and displays them on a MessageBox...

It will help !

Visual Basic:
Imports System.Math

...


Dim n As New ArrayList  'I use ArrayLists... I like them most...

    Public Sub Random5Numbers()
        Randomize()     'Initializes the random-number generator.

        For i As Integer = 0 To 4   'Puts the 5 numbers into the ArrayList
            n.Add(Round(Rnd() * 9))
        Next

        'Show the five numbers on a textbox, just for testing...
        MessageBox.Show(n(0) & ";" & n(1) & ";" & n(2) & ";" & n(3) & ";" & n(4))
    End Sub

From this I think it's easy to figure out the rest... :D
Otherwise... ask again !:D
 
ok, the book that I have doesnt really go into array list anywhere in the book, so I would perfer to stay away from them.

Looking at your code, and knowing what I know I came to this

Code:
    Public Sub Random5Numbers()
        Randomize()     'Initializes the random-number generator.

        For count = 0 To 4   'Puts the 5 numbers into the Array
            Lotto(count) = Int(0 + Rnd() * (9 - 0))

        Next count

Would that work? I really didnt need the messagebox because I dont want to user to know what numbers have been seleceted.

and as far as having the user enter his 5 chocies what should i do ?
 
You'd be much better served by using the Random class.
Visual Basic:
'Initialize a new random number generator with a seed value
Dim rand As Random = New Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
'Minimum and maximum values to generate
Dim minValue As Integer = 0, maxValue As Integer = 10

'Initialize each element in array to a random number between 0 and 9 inclusive
Dim numbers() As Integer = {rand.Next(minValue, maxValue), _
    rand.Next(minValue, maxValue), _
    rand.Next(minValue, maxValue), _
    rand.Next(minValue, maxValue), _
    rand.Next(minValue, maxValue)}

Dim counter As Integer

'Enumerate through the array
For counter = numbers.GetLowerBound(0) To numbers.GetUpperBound(0)
    MessageBox.Show(numbers(counter).ToString, Application.ProductName)
Next
 
Back
Top