thankins Posted November 9, 2003 Posted November 9, 2003 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 Quote
thankins Posted November 9, 2003 Author Posted November 9, 2003 here is what I have so far. 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 Quote
AlexCode Posted November 9, 2003 Posted November 9, 2003 This piece of code it's a routine that generates 5 numbers into a ArrayList and displays them on a MessageBox... It will help ! 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 Quote Software bugs are impossible to detect by anybody except the end user.
thankins Posted November 9, 2003 Author Posted November 9, 2003 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 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 ? Quote
*Gurus* Derek Stone Posted November 9, 2003 *Gurus* Posted November 9, 2003 You'd be much better served by using the Random class. '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 Quote Posting Guidelines
samsmithnz Posted December 11, 2003 Posted December 11, 2003 Excellent Example. Just found this searching and wanted to say thanks! Quote Thanks Sam http://www.samsmith.co.nz
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.