This tutorial will introduce you to the foundation of all things random — the simple random number.
Generating Random Numbers
Random numbers are generated using a function that generates random numbers.
PlayBasic has 4 functions that generate random numbers :
Random Integers
Rnd() generates a random integer between 0 and it's argument.
In other words, if you wanted to generate a number between 0 and 10 you'd use Rnd() like this :
rndInt = Rnd(10)
Print rndInt
Sync
WaitKey
If you already know s BASIC dialect other than PlayBasic, you'll expect Rnd() to generate a number with a decimal fraction. Rnd#() does this. See Generating Random Floats below.
Range of Random Integers
RndRange() generates a range of random integers.
To generate random range of numbers between 5 and 10, you'd use RndRange() this way :
rndInt = RndRange( 5, 10 )
Print rndInt
Sync
WaitKey
Generating Random Floats
Rnd#() generates a float between 0 and a number below it's argument.
In other words, if you wanted to generate a number between 0 and 9.99_, you'd use Rnd#() this way :
rndFloat# = Rnd#(10)
Print rndFloat#
Sync
WaitKey
Range of Random Floats
RndRange#() generates a float between two numbers. The high number will always be just below it's second argument, as with Rnd#().
To generate a random float between 5 and 9.99_ :
rndFloat# = Rnd#( 5, 10 )
Print rndFloat#
Sync
WaitKey
| Categories: Beginner Tutorials : Math : Math Tutorials : Programming Tutorials : Tutorials |