func_maths_rnddouble.rst 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. .. _func_maths_rnddouble:
  2. =========
  3. RndDouble
  4. =========
  5. RndDouble -
  6. Description
  7. ===========
  8. .. code-block:: blitzmax
  9. RndDouble!()
  10. Generate random double
  11. Parameters
  12. ==========
  13. Return Values
  14. =============
  15. A random double in the range 0 (inclusive) to 1 (exclusive)
  16. Examples
  17. ========
  18. .. code-block:: blitzmax
  19. ' RndDouble.bmx
  20. ' Two players take turns shooting at a target. The first hit wins.
  21. ' Player 1 hits 30% of the time, player 2 hits 40%.
  22. ' What is the probability that player 1 wins?
  23. Function winner() ' play game once, return winner 1 or 2
  24. Repeat
  25. If RndDouble() < 0.3 Then Return 1
  26. If RndDouble() < 0.4 Then Return 2
  27. Forever
  28. End Function
  29. Local count[3]
  30. trials = 1000000
  31. For n = 1 to trials
  32. count[ winner() ] :+ 1
  33. Next
  34. Print "Estimated probability = " + ( Double( count[1] ) / Double( trials ) )
  35. Print
  36. Print " Exact probability = " + ( 15.0 / 29.0 )
  37. Input
  38. End
  39. See Also
  40. ========