Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: A little basic help with randum numbers in VB .NET  (Read 5163 times)

0 Members and 1 Guest are viewing this topic.

Valeriee

  • Guest
A little basic help with randum numbers in VB .NET
« on: July 22, 2006, 08:01:42 AM »
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!

Dilbert

  • Moderator


  • Egghead

  • Welcome to ComputerHope!
  • Thanked: 44
    Re: A little basic help with randum numbers in VB
    « Reply #1 on: July 22, 2006, 08:43:44 PM »
    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.asp
    "The geek shall inherit the Earth."

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: A little basic help with randum numbers in VB
    « Reply #2 on: July 23, 2006, 05:49:37 AM »
    Not 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-)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein