PFS! Computer Science

9.0. Overview
The random module, and functions with returns

In this discussion we'll learn how to make the computer produce random numbers (good for playing games and writing simulations), and learn more about writing functions.

9.1. The random Module

There are lots of situations where you'll need to simulate a random number: flipping a coin, rolling a die, picking a card from a deck, simulating the spread of a disease, etc. For programs that need a random number, you'll use the Math.random() function that comes with JavaScript.

The Math.random Module

To generate a random number between 0 and 0.999999999999999, use the function Math.random().

Example:

document.write(Math.random());
document.write(Math.random());
document.write(Math.random());

Produces the result:
0.1581813084032696
0.4691701847918265
0.6778300550325308

That's not usually the kind of number you're looking for, though. Usually you want something like a random number between 1 and 6 (for rolling a die). In that case:

let roll = Math.floor(Math.random() * 6 + 1);

How to get a specified random number range

It's a little tricky, figuring out how to specify a formula that will give you the correct random number range.

Here's how it works:

  1. Math.random() → a random number from 0 - 0.999999999
  2. Math.random() * 6 → a random number = 6 * (0 - 0.99999999), or 0 - 5.99999999994
  3. Math.random() * 6 + 1 → a random number = 1 - 6.99999999994
  4. Math.floor(Math.random() * 6 + 1) → the "rounded-down-to-the-nearest-integer" value of 1 - 6.99999999994, or 1 - 6

9.2. Some dice-rolling functions

Now that we know how to produce a random number, let's write some functions that will simulate rolling dice.

Die Roller 1

Write a short JavaScript program that uses a function called roll() to print out the simulated result of rolling a six-sided die.

Show/hide script

<script>
    // define the function
    function roll()
    {
        let result = Math.floor(Math.random() * 6 + 1);
        document.write(result);
    }
    
    // call the function
    roll();
</script>

Rolling a six-sided die is fun, but some games have dice with another number of sides: 4, 8, 10, 12, 20...

Die Roller 2

Write a short JavaScript program that uses a function called roll(n) to print out the simulated result of rolling an n-sided die.

Show/hide script

<script>
    // define the function
    function roll(n)
    {
        let result = Math.floor(Math.random() * n + 1);
        document.write(result);
    }
    
    // call the function to roll a seven-sided (?) die
    roll(7);
</script>

Rolling a single die is fine, but in a lot of games, you have to roll two dice and add the numbers together.

Die Roller 3

Write a short JavaScript program that uses a function called rollDice() to print out the simulated result of rolling two six-sided dice.

Show/hide script

<script>
    // define the function
    function roll()
    {
        let result1 = Math.floor(Math.random() * 6 + 1);
        let result2 = Math.floor(Math.random() * 6 + 1);
        document.write(result1 + result2);
    }
    
    // call the function
    roll();
</script>

9.3. Functions that return values

It is often the case that we want to write a function that will calculate something, but we don't want to print it out; maybe we just need to use that value in another part of our program.

(Imagine that you were writing a program that has the computer play poker with the user. You'd want the computer to be able to randomly deal cards to everybody, without telling all the players who has what cards.)

In this case, you write the function and return a value to the caller.

// This function calculates a secret number between 1 and 10
function getSecretNumber()
{
    let secret = Math.floor(Math.random() * 10 + 1);
    return secret;
}

In the main program, we would take that value and save it someplace for later use:

let hiddenValue = getSecretNumber(); // stores a value 1-10

Let's write one last dice-roller program.

Die Roller 4

Write a short JavaScript program that uses a function called rollDie() to calculated a simulated result of rolling a six-sided die, and then return that value to the "caller" (the main part of the script). Then print out the result.

Show/hide script

<script>
    // define the function
    function rollDie()
    {
        let result = Math.floor(Math.random() * 6 + 1);
        return result;
    }
    
    // call the function
    let theDie = rollDie();
    document.write(theDie);
</script>