praxis

Various programming exercises.
git clone git://git.jtobin.io/praxis.git
Log | Files | Refs

monty.r (284B)


      1 #!/usr/bin/Rscript
      2 monty = function(n, switch) {
      3     z  = rmultinom(n, 3, replicate(3, 1/3))
      4     zs = apply(z, MARGIN = 1, sum)
      5     if (switch) { wins = sum(zs[1:2]) } else { wins = zs[3] }
      6     print(wins / sum(zs))
      7 }
      8 
      9 whenSwitching = monty(10000, T)
     10 whenStaying   = monty(10000, F)
     11