|
-
Apr 2nd, 2010, 01:34 AM
#1
Thread Starter
New Member
vb code for Monty Hall problem not behaving in the desired way
Hi,
I have written a vb code for the Monte Hall Problem(in my case I have used the example of 3 cards - one of the card is red and 2 are black, you have to choose red to win) But I am not getting the desired results as expected.
Could someone point out my mistakes or advice me where I have gone wrong in my code.
I have attached my vb code with this thread.
vb.net Code:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ThreeCardMonte()
End Sub
Sub ThreeCardMonte()
Randomize()
Dim cards(0 To 2) As Integer ' 1 means a red card , 0 means a black card anywhere in the 3 card stack
Dim switchwins As Integer = 0
Dim staywins As Integer = 0
Dim winner_redcard As Integer
Dim games As Integer
Dim choice As Integer
Dim shown_card As Integer
For games = 1 To 100
Randomize()
winner_redcard = CInt(Int((2 * Rnd()) + 0))
cards(winner_redcard) = 1 ' randomly place the winning red card in the card stack
choice = CInt(Int((2 * Rnd()) + 0))
Do
shown_card = CInt(Int((2 * Rnd()) + 0))
Loop While ((cards(shown_card) = 1) Or (shown_card = choice))
staywins = staywins + Int(cards(choice)) ' the case where you won by staying with your choice
Dim temp As Integer
temp = choice + shown_card
switchwins = switchwins + Int(cards(2 - (temp))) ' the case where win by switching
cards(winner_redcard) = 0 ' reset the winning card for the next round of game
Next
Debug.Print("Switchwins = ")
Debug.Print(switchwins)
Debug.Print("Staywins = ")
Debug.Print(staywins)
End Sub
End Class
-
Apr 2nd, 2010, 06:45 AM
#2
Re: vb code for Monty Hall problem not behaving in the desired way
-
Apr 2nd, 2010, 07:36 AM
#3
Fanatic Member
Re: vb code for Monty Hall problem not behaving in the desired way
is this not vb6? can you use rnd with .net :s
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Apr 2nd, 2010, 08:07 AM
#4
Re: vb code for Monty Hall problem not behaving in the desired way
That's VB6 code: Debug.Print
-tg
-
Apr 2nd, 2010, 08:11 AM
#5
Re: vb code for Monty Hall problem not behaving in the desired way
"But I am not getting the desired results as expected." -- Ahh... the old eel toast problem... this is a classic problem around here. The problem is that you've got eels in your hovercraft. When this happens, is causes you get toast when you asked for OJ. Other known symptoms include the wheels falling off of your car, a door falling off its hinge, and worst of all an arm falling off.
We aren't mind readers, we can't possibly know what the expected results were supposed to be, and we have no way of knowing what the actual results were, so we couldn't tell you how to begin to fix it. Oh, wait... yes there is a way we can... put a break point on the first line, step through the code one line at a time, and check your values. Are they what they should be? Are they what you expect them to be? Does the logic flow properly?
-tg
-
Apr 2nd, 2010, 10:30 AM
#6
Hyperactive Member
Re: vb code for Monty Hall problem not behaving in the desired way
Well if you just intuitively walk through the final results of the scenarios, you can save yourself alot of computing power
Code:
Dim lose As Integer = 0, win As Integer = 0
For x = 0 To 100000
Dim r As New Random
Dim winner As Integer = r.Next(1, 4)
Dim pick As Integer = r.Next(1, 4)
Dim shown As Integer = 0
If winner = pick Then
lose += 1
Else
win += 1
End If
Next
MessageBox.Show("Wins: " & win & vbCrLf & "Loses: " & lose)
-
Apr 2nd, 2010, 12:08 PM
#7
Thread Starter
New Member
Re: vb code for Monty Hall problem not behaving in the desired way
I am sorry for not letting you guys know what results I am expecting. I should be getting the number of switchwins as 2/3 of the total number of games played and number of staywins as 1/3 of total number of games played.
Also moderator this vb6 code , could you move it to the vb section ?
Thanks
-
Apr 2nd, 2010, 01:35 PM
#8
Re: vb code for Monty Hall problem not behaving in the desired way
What are you actually writting in? VB.NET or VB6? I see some of both in your code.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 2nd, 2010, 01:37 PM
#9
Re: vb code for Monty Hall problem not behaving in the desired way
How do you get VB6 out of this?
Code:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
I won't even go into the rest of the code.
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
|