rndfloat.bmx 592 B

12345678910111213141516171819202122232425
  1. ' RndFloat.bmx
  2. ' Two players take turns shooting at a target. The first hit wins.
  3. ' Player 1 hits 30% of the time, player 2 hits 40%.
  4. ' What is the probability that player 1 wins?
  5. Function winner() ' play game once, return winner 1 or 2
  6. Repeat
  7. If RndFloat() < 0.3 Then Return 1
  8. If RndFloat() < 0.4 Then Return 2
  9. Forever
  10. End Function
  11. Local count[3]
  12. trials = 1000000
  13. For n = 1 to trials
  14. count[ winner() ] :+ 1
  15. Next
  16. Print "Estimated probability = " + ( Float( count[1] ) / Float( trials ) )
  17. Print
  18. Print " Exact probability = " + ( 15.0 / 29.0 )
  19. Input ; End