RosettaCodeData/Task/100-prisoners/FreeBASIC/100-prisoners.basic
2023-07-01 13:44:08 -04:00

42 lines
1.3 KiB
Text

#include once "knuthshuf.bas" 'use the routines in https://rosettacode.org/wiki/Knuth_shuffle#FreeBASIC
function gus( i as long, strat as boolean ) as long
if strat then return i
return 1+int(rnd*100)
end function
sub trials( byref c_success as long, byref c_fail as long, byval strat as boolean )
dim as long i, j, k, guess, drawer(1 to 100)
for i = 1 to 100
drawer(i) = i
next i
for j = 1 to 1000000 'one million trials of prisoners
knuth_up( drawer() ) 'shuffles the cards in the drawers
for i = 1 to 100 'prisoner number
guess = gus(i, strat)
for k = 1 to 50 'each prisoner gets 50 tries
if drawer(guess) = i then goto next_prisoner
guess = gus(drawer(guess), strat)
next k
c_fail += 1
goto next_trial
next_prisoner:
next i
c_success += 1
next_trial:
next j
end sub
randomize timer
dim as long c_fail=0, c_success=0
trials( c_success, c_fail, false )
print using "For prisoners guessing randomly we had ####### successes and ####### failures.";c_success;c_fail
c_success = 0
c_fail = 0
trials( c_success, c_fail, true )
print using "For prisoners using the strategy we had ####### successes and ####### failures.";c_success;c_fail