100 prisoners' problem
#10
In case anyone besides me wants to take a peek into the cycles created by random shuffles of 100 numbers here is my "100 Prisoners Group Analysis"

Code: (Select All)
_Title "100 Prisoners Group Analysis" ' b+ 2023-04-17
$Console:Only
Randomize Timer
Dim As Long slots(1 To 100), i, p, visit, c, cycleN

For i = 1 To 100
    slots(i) = i
Next
Do
    GoSub shuffle
    Cls
    ReDim As Long cycle(1 To 100), count(1 To 100)
    cycleN = 0
    For p = 1 To 100 ' prisoner number
        If cycle(p) = 0 Then
            c = 1
            cycleN = cycleN + 1 ' new cycle number
            cycle(p) = cycleN
            visit = slots(p)
            Print "Cycle Number:"; cycleN; " first visit "; visit;
            While visit <> p
                cycle(visit) = cycleN ' assign cycle number to number visit
                c = c + 1
                visit = slots(visit) ' get next number to visit
                Print visit;
            Wend
            count(cycleN) = c
            Print: Print
        End If
    Next
    Print " Note: these cycles end on the prisoner number that started cycle."
    Print: Print
    For i = 1 To cycleN
        Print "Cycle Number:"; i; " Visit Count:"; count(i)
    Next
    Print: Print "     ...zzz"
    Sleep
Loop Until _KeyDown(27)
End
shuffle:
For i = 100 To 2 Step -1
    Swap slots(Int(Rnd * i) + 1), slots(i)
Next
Return

Sample Output of a Run:
   
Here Prisoner 3 failed to find es number in under 50 draws.
b = b + ...
Reply


Messages In This Thread
100 prisoners' problem - by TempodiBasic - 04-15-2023, 10:44 AM
RE: 100 prisoners' problem - by bplus - 04-15-2023, 02:18 PM
RE: 100 prisoners' problem - by SMcNeill - 04-15-2023, 02:31 PM
RE: 100 prisoners' problem - by TempodiBasic - 04-16-2023, 11:04 AM
RE: 100 prisoners' problem - by SMcNeill - 04-16-2023, 02:41 PM
RE: 100 prisoners' problem - by bplus - 04-15-2023, 05:41 PM
RE: 100 prisoners' problem - by SMcNeill - 04-15-2023, 07:08 PM
RE: 100 prisoners' problem - by bplus - 04-16-2023, 02:32 PM
RE: 100 prisoners' problem - by SMcNeill - 04-16-2023, 03:25 PM
RE: 100 prisoners' problem - by bplus - 04-17-2023, 07:30 PM



Users browsing this thread: 4 Guest(s)