rnd.bmx 535 B

12345678910111213141516171819
  1. ' Rnd.bmx
  2. ' Use Rnd() to estimate area inside the unit circle x^2 + y^2 = 1.
  3. totalpoints = 1000000
  4. For n = 1 to totalpoints
  5. x! = Rnd( -1.0, 1.0 ) ' Generate random point in 2 by 2 square.
  6. y! = Rnd( -1.0, 1.0 )
  7. If x*x + y*y < 1.0 Then inpoints :+ 1 ' point is inside the circle
  8. Next
  9. ' Note: Ratio of areas circle/square is exactly Pi/4.
  10. Print "Estimated area = " + ( 4.0 * Double(inpoints)/Double(totalpoints) )
  11. Print
  12. Print " Exact area = " + Pi ' 4 * Pi/4, compare with estimate
  13. Input ; End