Chapter 2. Making Decisions with Conditions
In This Chapter
Generating random numbers and integers
Working with conditions
Handling unusual conditions
One of the most important aspects of computers is their apparent ability to make decisions. Computers can change their behavior based on circumstances. In this chapter, you discover how to maximize this decision-making ability.
Working with Random Numbers
Math.random() function returns a random floating point value between zero and one.
Creating an integer within a range
Creating a random floating point number between zero and one is easy, thanks to the
Math.random() function. What if you want an integer within a specific range? For example, say that you want to simulate rolling a six-sided die. How do you get from the 0-to-1 floating point value to a 1-to-6 integer?
Here's the standard approach:
Get a random float between ...