Results 1 to 7 of 7

Thread: Have a listbox randomly pick to remove either the odd numbers or the even numbers

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Have a listbox randomly pick to remove either the odd numbers or the even numbers

    Hi there folks. I got this math game to help show students the difference between odd numbers and even numbers between 1-25, but I was wondering if there was a way for vb6 to remove either all the odd numbers, or all the even numbers from a list box called numlist.
    But the trick is I would like the program to do it randomly, choose either even or odds.

    Now I can have the listbox remove them by going

    numlist.removeitem - the position of the number, as they are all in order from 1-25., but I'm stuck on trying to get it to pick at random either odds or evens. Has anyone ever done something like that before?

    THanks!

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers

    Of course. The key is to remove them in reverse order

    You could do a loop high to low with a step of -2

    Generate a random number from 0 to 1

    If you want to remove the even numbers then your loop may be
    For x = 24 to 0 Step -2

    For odd it may be
    For x=25 to 1 Step -2

    in the loop you would remove the item at position x

    Both assume that 0 is included in the list.

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers

    You realize, of course, that if the computer determines at random whether the numbers being removed are going to be odd or even, it could remove nothing but odd numbers for many executions in succession and you would never see evens removed and vice versa. The odds are slim that you would get more than 5 or 6 odd or even list removals in a row, but it could happen. Ask any gambler playing roulette.

    I suggest that you have two command buttons--one for odd number removal and the other for even number removal. Add a third for "Odd/Even at Random". BTW, if there are always the numbers 1 through 25, then the same set will be removed (either odd or even) every time. So, the excerise seems trivial to me, unless you want to also show a second list revealing the order in which they were removed and remove them on a random selection basis.
    Doctor Ed

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers

    Quote Originally Posted by Code Doc View Post
    You realize, of course, that if the computer determines at random whether the numbers being removed are going to be odd or even, it could remove nothing but odd numbers for many executions in succession and you would never see evens removed and vice versa.
    Also, the original poster would need to check whether or not a number has already been removed so the program doesn't crash.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers

    Quote Originally Posted by Nightwalker83 View Post
    Also, the original poster would need to check whether or not a number has already been removed so the program doesn't crash.
    I'm not even sure that OP has the problem defined correctly or knows exactly what he wants to display. The random sequence of removal would vary but the eventual removed set of either all the odds or all the evens is always going to be identical.
    Odds: 1,3,5,7,9,11,13,15,17,19,21,23,25
    Evens: 2,4,6,8,10,12,14,16,18,20,22,24
    Doctor Ed

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers

    Will this work for you?

    Code:
    Private Sub cmdRemove_Click()
        Dim i As Integer
    
        With numlist
            If .ListCount Then
                i = Int(.ListCount * Rnd)
                Caption = "Removed = " & .List(i)
               .RemoveItem i
            End If
        End With
    End Sub
    
    Private Sub Form_Load()
        Dim i As Integer
    
        For i = 1 To 25
            numlist.AddItem i
        Next
        Randomize
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers

    Quote Originally Posted by Nightwalker83 View Post
    Also, the original poster would need to check whether or not a number has already been removed so the program doesn't crash.
    Not really, as I mentioned in my post the key is reverse order removal high to low
    The number will be the same as the list inxed so you would simply remove the highest number first and work down to the first. There is no chance of hitting the same number twice nor crashing the program using this method which would be fast and simple.

    I also am not sure what the point would be but the task is simple.

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