(CGA) Using Random Lesson
Using Random
Game designers use random techniques so the game will not be the same every time you play. Random allows the program to randomly pick a value from a range of values.
In Greenfoot, the getRandomNumber() method is used to generate random numbers, within a specified range, to create randomly generated behavior.
The method is in the Greenfoot class.
It is a static method that returns a random number between zero and a parameter limit -1.
Any method used from the Greenfoot class must be called by the class using dot notation.
Dot notation format
- Name of class or object to which the method belongs
- Dot
- Name of method to call
- Parameter list
- Semicolon
Example
Greenfoot.getRandomNumber(30);
The Greenfoot.getRandomNumber(int limit)
This returns a random number between 0 (inclusive) and the limit (exclusive)
Greenfoot.getRandomNumber(30);
This would produce a number from 0 (inclusive) and 30 (exclusive). In other words, it would produce random numbers in the range of 0 to 29.
It can be used in various ways.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION