Comp 101 Generating Random Numbers

Sometimes in our code we want to use a random number, instead of a number we choose ourselves. Luckily for us, the introcs library has an in-built function called random which will produce a random number when we call it. 

Like our print function, we have to import our random function using:

import { random } from "introcs";

Once we've imported it, we can use it in place of any number in our code, as it generates a random number. To call the function, we would use the function name, in this case random, followed by the range of the random we want in ( )

For example, say we wanted a variable x to be a random number between 1 and 12, we would use our random function like this:

let x: number = random(1, 12);
How would we generate a random number between 17 and 32?