1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- .. _func_maths_rndfloat:
- ========
- RndFloat
- ========
- RndFloat -
- Description
- ===========
- .. code-block:: blitzmax
- RndFloat#()
- Generate random float
- Parameters
- ==========
- Return Values
- =============
- A random float in the range 0 (inclusive) to 1 (exclusive)
- Examples
- ========
- .. code-block:: blitzmax
- ' RndFloat.bmx
- ' Two players take turns shooting at a target. The first hit wins.
- ' Player 1 hits 30% of the time, player 2 hits 40%.
- ' What is the probability that player 1 wins?
-
- Function winner() ' play game once, return winner 1 or 2
- Repeat
- If RndFloat() < 0.3 Then Return 1
- If RndFloat() < 0.4 Then Return 2
- Forever
- End Function
-
- Local count[3]
-
- trials = 1000000
-
- For n = 1 to trials
- count[ winner() ] :+ 1
- Next
-
- Print "Estimated probability = " + ( Float( count[1] ) / Float( trials ) )
- Print
- Print " Exact probability = " + ( 15.0 / 29.0 )
-
- Input
- End
- See Also
- ========
|