Results 1 to 3 of 3

Thread: [2005]

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    1

    Exclamation [2005]

    In this simple Lottery simulation, how do I ensure that a number is not generated twice? I am assuming I need to search the array to verify that there are no equal values but how? Thanks much.

    Public Class Lottery

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim counter As Integer = 0

    Dim y As Integer

    Dim instance As New Random()

    Dim myArray(5) As Integer


    While counter < myArray.Length

    y = instance.Next(1, 37)

    myArray(counter) = y

    counter = counter + 1

    TextBox1.Text = TextBox1.Text & y & (" ")

    End While

    End Sub

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005]

    try this code


    Code:
    dim counter as integer = 0 
    dim numberArray = new arraylist()
    dim i, y as integer
    dim instance as new Random()
    
    while numberArray.count < 6
         y = instance.Next(1, 37)
         if numberArray.indexOf(y) > -1 then continue while
         numberArray.Add(y)
    end while

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005]

    In a lottery draw what do they do when they pick a number? Do they put it back so it can be picked again? No, they remove it from the pool so they know that every selection will be unique. You should do the same thing.

    http://www.vbforums.com/showthread.php?t=393023

    Also, please provide a descriptive title for your threads.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width