How to resolve the algorithm 100 prisoners step by step in the Visual Basic .NET programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 100 prisoners step by step in the Visual Basic .NET programming language

Table of Contents

Problem Statement

Show and compare the computed probabilities of success for the two strategies, here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 100 prisoners step by step in the Visual Basic .NET programming language

Source code in the visual programming language

Module Module1

    Function PlayOptimal() As Boolean
        Dim secrets = Enumerable.Range(0, 100).OrderBy(Function(a) Guid.NewGuid).ToList

        For p = 1 To 100
            Dim success = False

            Dim choice = p - 1
            For i = 1 To 50
                If secrets(choice) = p - 1 Then
                    success = True
                    Exit For
                End If
                choice = secrets(choice)
            Next

            If Not success Then
                Return False
            End If
        Next

        Return True
    End Function

    Function PlayRandom() As Boolean
        Dim secrets = Enumerable.Range(0, 100).OrderBy(Function(a) Guid.NewGuid).ToList

        For p = 1 To 100
            Dim choices = Enumerable.Range(0, 100).OrderBy(Function(a) Guid.NewGuid).ToList

            Dim success = False
            For i = 1 To 50
                If choices(i - 1) = p Then
                    success = True
                    Exit For
                End If
            Next

            If Not success Then
                Return False
            End If
        Next

        Return True
    End Function

    Function Exec(n As UInteger, play As Func(Of Boolean))
        Dim success As UInteger = 0
        For i As UInteger = 1 To n
            If play() Then
                success += 1
            End If
        Next
        Return 100.0 * success / n
    End Function

    Sub Main()
        Dim N = 1_000_000
        Console.WriteLine("# of executions: {0}", N)
        Console.WriteLine("Optimal play success rate: {0:0.00000000000}%", Exec(N, AddressOf PlayOptimal))
        Console.WriteLine(" Random play success rate: {0:0.00000000000}%", Exec(N, AddressOf PlayRandom))
    End Sub

End Module


  

You may also check:How to resolve the algorithm Conditional structures step by step in the F# programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Pop11 programming language
You may also check:How to resolve the algorithm A+B step by step in the Rust programming language
You may also check:How to resolve the algorithm File modification time step by step in the Objeck programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Ruby programming language