Results 1 to 9 of 9

Thread: vb code for Monty Hall problem not behaving in the desired way

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    2

    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:
    1. Public Class Form2
    2.  
    3.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.         ThreeCardMonte()
    6.  
    7.     End Sub
    8.  
    9.     Sub ThreeCardMonte()
    10.         Randomize()
    11.         Dim cards(0 To 2) As Integer ' 1 means a red card , 0 means a black card anywhere in the 3 card stack
    12.         Dim switchwins As Integer = 0
    13.         Dim staywins As Integer = 0
    14.         Dim winner_redcard As Integer
    15.         Dim games As Integer
    16.         Dim choice As Integer
    17.         Dim shown_card As Integer
    18.  
    19.         For games = 1 To 100
    20.             Randomize()
    21.  
    22.             winner_redcard = CInt(Int((2 * Rnd()) + 0))
    23.             cards(winner_redcard) = 1 ' randomly place the winning red card in the card stack
    24.             choice = CInt(Int((2 * Rnd()) + 0))
    25.  
    26.             Do
    27.                 shown_card = CInt(Int((2 * Rnd()) + 0))
    28.             Loop While ((cards(shown_card) = 1) Or (shown_card = choice))
    29.  
    30.             staywins = staywins + Int(cards(choice))  ' the case where you won by staying with your choice
    31.             Dim temp As Integer
    32.             temp = choice + shown_card
    33.             switchwins = switchwins + Int(cards(2 - (temp)))  ' the case where win by switching
    34.             cards(winner_redcard) = 0 ' reset the winning card for the next round of game
    35.         Next
    36.  
    37.         Debug.Print("Switchwins = ")
    38.         Debug.Print(switchwins)
    39.         Debug.Print("Staywins = ")
    40.         Debug.Print(staywins)
    41.  
    42.     End Sub
    43. End Class

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: vb code for Monty Hall problem not behaving in the desired way

    Moved To VB.NET

  3. #3
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    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.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: vb code for Monty Hall problem not behaving in the desired way

    That's VB6 code: Debug.Print

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    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)

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    2

    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

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width