Random
Add chaos to your program by learning about randomization

 

How to Program Random

With programming we usually want things to happen the same every time through the loop, but there are cases when we want things to be randomized.

To create a random value we have two different ways to do it.

or

Note that min and max are to be replaced with actual numbers in your code. The first will return a value from 0 up to (but not including) max. Then the second one will return a value between min and max. Both of these can pick the lowest number (0 or min), but it will never pick the max  value. We can then use these values to make random decisions. For example:

This one will randomly pick a number between 0 and 4. Then using if statements (if you want to learn more checkout this tutorial) it will print a different message depending on the random choice. It will then wait half a second and repeat this process.

Here is some example output:
Good night
Bye
Bye
Good morning
Good morning
Hi
Hi
Good morning
Good night
Bye
Hi
Bye
Hi
Good morning
Good night