Random numbers can be very useful in your Small Basic games. They can be used to make the game play vary from one game to the next.
The Small Basic keyword to get a random number is Math.GetRandomNumber(maxNumber). It will return a random integer between 1 and the input value maxNumber.
Here are a few ideas and tips:
We can position sprites with random positions and velocities.
We can randomly do different things in the code, perhaps equivalent to rolling a dice.
"Do something for a dice roll of 1"
"Do something for a dice roll of 2"
"Do something for a dice roll of 3"
"Do something for a dice roll of 4"
"Do something for a dice roll of 5"
"Do something for a dice roll of 6"
We could also only do something with a probability, say 1 in 10.
"This is a 1 in 10 event!!!"
"A 10% probability event didn't happen"
If we want random numbers over a different range (not 1 to maxNumber) or with smaller intervals, for example 0.2, then we can use the following formula.
Here is an example using this to create random numbers between -5 and 3, with intervals of 0.2.