func_maths_rnd.rst 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. .. _func_maths_rnd:
  2. ===
  3. Rnd
  4. ===
  5. Rnd -
  6. Description
  7. ===========
  8. .. code-block:: blitzmax
  9. Rnd!( min_value!=1,max_value!=0 )
  10. Generate random double
  11. The optional parameters allow you to use Rnd in 3 ways:
  12. [ @Format | @Result
  13. * &Rnd() | Random double in the range 0 (inclusive) to 1 (exclusive)
  14. * &Rnd(_x_) | Random double in the range 0 (inclusive) to n (exclusive)
  15. * &Rnd(_x,y_) | Random double in the range x (inclusive) to y (exclusive)
  16. ]
  17. Parameters
  18. ==========
  19. Return Values
  20. =============
  21. A random double in the range min (inclusive) to max (exclusive)
  22. Examples
  23. ========
  24. .. code-block:: blitzmax
  25. ' Rnd.bmx
  26. ' Use Rnd() to estimate area inside the unit circle x^2 + y^2 = 1.
  27. totalpoints = 1000000
  28. For n = 1 to totalpoints
  29. x! = Rnd( -1.0, 1.0 ) ' Generate random point in 2 by 2 square.
  30. y! = Rnd( -1.0, 1.0 )
  31. If x*x + y*y < 1.0 Then inpoints :+ 1 ' point is inside the circle
  32. Next
  33. ' Note: Ratio of areas circle/square is exactly Pi/4.
  34. Print "Estimated area = " + ( 4.0 * Double(inpoints)/Double(totalpoints) )
  35. Print
  36. Print " Exact area = " + Pi ' 4 * Pi/4, compare with estimate
  37. Input
  38. End
  39. See Also
  40. ========