6.1 Whole Numbers

The function Random() returns a random number between 0 and 4.294.967.295. Check this yourself with the next example:

 
uint number = 0; 
 
bool Update() { 
  if(Kb.bp(KB_SPACE)) number = Random(); 
5  return true
} 
 
void Draw() { 
  D.clear(BLACK); 
10  D.text(0, 0, S + number); 
}  

You will rarely need a number this big. Which is why you can use the Random() function with one or more arguments. When used with one argument, the function will return a number in the range 0 to the argument minus one. In other words, Random(5) will return one of the values 0, 1, 2, 3 or 4. Count them, that’s 5 different values. A common beginners mistake is to expect the number five as a result. That will never, ever happen!

It is also possible to pass two arguments. Random(-2, 4) returns one of these values: -2, -1, 0, 1, 2, 3 or 4. The important thing to remember that with this version, the arguments are inclusive.

Exercise
Create the basics of a lottery application. Show a new number from 1 to 42 (inclusive) on the screen every time you press the space bar.

Note
How to get a random color? The RGB values which make up a color have a value between 0 and 255. To randomize a color, try this:
 
Color myColor; 
myColor.set(Random(256), Random(256), Random(256));