BRL.Random: | Functions | Modinfo | Source |
SeedRnd MilliSecs()This ensures that the random number generator does not start in the same state each time your program is run, which would cause it to produce the same sequence of random numbers.
Function Rand( min_value,max_value=1 ) | |||||||
Returns | A random integer in the range min (inclusive) to max (inclusive) | ||||||
Description | Generate random integer. | ||||||
Information | The optional parameter allows you to use Rand in 2 ways:
| ||||||
Example | ' Rand.bmx ' Toss a pair of dice. Result is in the range 1+1 to 6+6. ' Count how many times each result appears. Local count[13] For n = 1 To 3600 toss = Rand(1,6) + Rand(1,6) count[toss] :+ 1 Next For toss = 2 To 12 Print LSet(toss, 5)+count[toss] Next |
Function Rnd!( min_value!=1,max_value!=0 ) | |||||||||
Returns | A random double in the range min (inclusive) to max (exclusive) | ||||||||
Description | Generate random double. | ||||||||
Information | The optional parameters allow you to use Rnd in 3 ways:
| ||||||||
Example | ' Rnd.bmx ' Use Rnd() to estimate area inside the unit circle x^2 + y^2 = 1. totalpoints = 1000000 For n = 1 to totalpoints x! = Rnd( -1.0, 1.0 ) ' Generate random point in 2 by 2 square. y! = Rnd( -1.0, 1.0 ) If x*x + y*y < 1.0 Then inpoints :+ 1 ' point is inside the circle Next ' Note: Ratio of areas circle/square is exactly Pi/4. Print "Estimated area = " + ( 4.0 * Double(inpoints)/Double(totalpoints) ) Print Print " Exact area = " + Pi ' 4 * Pi/4, compare with estimate Input ; End |
Function RndDouble!() | |
Returns | A random double in the range 0 (inclusive) to 1 (exclusive) |
Description | Generate random double. |
Example | ' RndDouble.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 RndDouble() < 0.3 Then Return 1 If RndDouble() < 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 = " + ( Double( count[1] ) / Double( trials ) ) Print Print " Exact probability = " + ( 15.0 / 29.0 ) Input ; End |
Function RndFloat#() | |
Returns | A random float in the range 0 (inclusive) to 1 (exclusive) |
Description | Generate random float. |
Example | ' 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 |
Function RndSeed() | |
Returns | The current random number generator seed. |
Description | Get random number generator seed. |
Information | Use in conjunction with SeedRnd, RndSeed allows you to reproduce sequences of random numbers. |
Example | ' RndSeed.bmx and SeedRnd.bmx ( one example for both ) ' Get/Set random number seed. SeedRnd MilliSecs() seed=RndSeed() Print "Initial seed="+seed For k=1 To 10 Print Rand(10) Next Print "Restoring seed" SeedRnd seed For k=1 To 10 Print Rand(10) Next |
Function SeedRnd( seed ) | |
Description | Set random number generator seed. |
Example | ' RndSeed.bmx and SeedRnd.bmx ( one example for both ) ' Get/Set random number seed. SeedRnd MilliSecs() seed=RndSeed() Print "Initial seed="+seed For k=1 To 10 Print Rand(10) Next Print "Restoring seed" SeedRnd seed For k=1 To 10 Print Rand(10) Next |
Version | 1.04 |
---|---|
Author | Mark Sibly, Floyd |
License | Blitz Shared Source Code |
Copyright | Blitz Research Ltd |
Modserver | BRL |