1.

Solve : A little basic help with randum numbers in VB .NET?

Answer»

Hi guys!

WELL, I'm designing a LITTLE lottery number generator that picks a -user-specified amount of random numbers- -between a specified minimum and maximum range-, and for a user-specified number of games. These VALUES are then forwarded to a listbox where each ROW is a seperate game (with the game's random numbers displayed). I'm attempting to use a multidimensional array for this, but I've been running into a little bit of problems

Can anyone tell me what they think the problem with my code is? The values that are passed to the listbox all seem to be zeroes.

Here is the problematic array:

Dim intMini As Integer
Dim intMaxi As Integer
Dim Number As Integer
Dim rand As New Random
Dim Rows As Integer
Dim Cols As Integer
Dim intPicks As Integer
Dim intGames As Integer

intMini = CInt(txtMinNumber.Text)
intMaxi = CInt(txtMaxNumber.Text)
intPicks = CInt(txtNumbersPerPick.Text)
intGames = CInt(txtNumberOfGames.Text)

Dim ArrNumbers(intPicks, intGames) As Integer
For Rows = 0 To intPicks
For Cols = 0 To intfGames
N = rand.Next(intMaxi + 1) - intMini
lstOutput.Items.Add(ArrNumbers(Rows, Cols).ToString())
Next Cols
Next Rows



Thank you! I'll be ETERNALLY grateful!OK, it's 100+ degrees in here and debugging isn't high on my "want-to-do" list. However, I can give you a helpful bit of info: The Random class.

OK, for an example, I'm going to write code to display 5 message boxes to show random numbers between 1 and 10:

Code: [Select]Dim variable As New Random
Dim i As Integer

For i = 1 To 5
MessageBox.Show(variable.Next(1, 10))
Next
This should show 5 boxes with random numbers between 1 and 10.

If any other method is suggested anywhere, and this code snippet works, just use the Random class. There used to be this really lengthy formula for calling a pseudo-random number, but this is so much simpler that it's the only real feasible solution.

For more info on the VB.NET Random class, read this article:

http://www.winnershtriangle.com/RandomNumbersInDotNet.aspNot sure how you posted your code (typed or cut & paste), but there is a mispelling. Surprised the IDE didn't flag this:

Code: [Select]Dim intMini As Integer
Dim intMaxi As Integer
Dim Number As Integer
Dim rand As New Random
Dim Rows As Integer
Dim Cols As Integer
Dim intPicks As Integer
Dim intGames As Integer

intMini = CInt(txtMinNumber.Text)
intMaxi = CInt(txtMaxNumber.Text)
intPicks = CInt(txtNumbersPerPick.Text)
intGames = CInt(txtNumberOfGames.Text)

Dim ArrNumbers(intPicks, intGames) As Integer
For Rows = 0 To intPicks
For Cols = 0 To [highlight]intfGames[/highlight]
N = rand.Next(intMaxi + 1) - intMini
lstOutput.Items.Add(ArrNumbers(Rows, Cols).ToString())
Next Cols
Next Rows

Would also suggest using masked text boxes to cut down on the conversions to integers.

8-)



Discussion

No Comment Found