Intro to 3D Game Development

0 of 193 lessons complete (0%)

Scripting Part 3: Finishing the Game Loop

Generate a Random Number

You don’t have access to this lesson

Please register or sign in to access the course content.

Now that we can designate a treasure spot ourselves, let’s have the program pick one at random for us, so that we don’t know which one it is going to be. To do this, we will need to generate a random number using the Math library.

  1. In the randomizeTreasure function, delete the existing console.log.
  2. Create a new variable with let called numSpots, and set it to the number of elements in the digSpots array with digSpots.length.
  3. Create a new variable called randomNumber, and set it to Math.random(), which will give you a decimal number between 0 and 1.
  4. In a console.log, multiply these together.
  5. Open the Dev Tools console and see what values you get each time you press the Play button.