-
Jan 13th, 2013, 04:06 PM
#1
Thread Starter
PowerPoster
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!
-
Jan 13th, 2013, 04:29 PM
#2
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.
-
Jan 13th, 2013, 08:57 PM
#3
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.
-
Jan 13th, 2013, 09:01 PM
#4
Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers
Originally Posted by Code Doc
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
-
Jan 13th, 2013, 09:48 PM
#5
Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers
Originally Posted by Nightwalker83
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
-
Jan 13th, 2013, 11:29 PM
#6
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)
-
Jan 13th, 2013, 11:39 PM
#7
Re: Have a listbox randomly pick to remove either the odd numbers or the even numbers
Originally Posted by Nightwalker83
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|