title: Javascript - Math.random function returns the same result while called in a for loop
body: <p>I have a <code>function{computerPlay()}</code> to generate a new random number every time it is called. It returns an item from the array declared at the top. It works how I'd like it to when I test it in the <code>console</code>. However, in order for my game to work properly, I've put it in a <code>for</code> loop and it keeps returning the same value unless I refresh the page. I can't find an answer anywhere and this is my first question. I'm sure the code is sloppy so please ignore that. Here is the code.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  let playOptionsArray = ["Rock", "Paper", "Scissors"];
  let totalTies = 0;
  let playerPoints = 0;
  let computerPoints = 0;
  let round = 1;
  
  //formats a player's entry so it will match playOptionsArray
    function firstLetterUppercase(string){
      let firstLetter = string.charAt(0);
      let firstLetterCapitalized = firstLetter.toUpperCase();
      let restOfResponse = string.slice(1);
      let formattedResponse = firstLetterCapitalized + restOfResponse;
      return formattedResponse;
    }
  
  //compares player entry to ensure it matches an item on the playOptions array
    function checkPlayerResponse(response){
      if (response === "Rock" || response === "Paper" || response === "Scissors") {
        return response;
      } else {
        return alert("You must choose either 'Rock', 'Paper', or 'Scissor' as your play. Please try again.");
      }
    }
  
  //generates a random option for the computer's play
  
    function computerPlay() {
      let computerSelection = playOptionsArray[Math.floor(Math.random()*(playOptionsArray.length))];
      return computerSelection;
    }
  
    let computerSelection = computerPlay();
  
    function getPlayerSelection() {
      let playerEntry = window.prompt("You are playing the computer in a game of Rock, Paper, Scissors. First player to five points wins. Choose either Rock, Paper, or Scissors as your play", "Rock");
      if (playerEntry != null) {
      let playerEntryLowercase = playerEntry.toLowerCase();
      playerSelection = firstLetterUppercase(playerEntryLowercase);
    } else {
      getPlayerSelection();
    }
      checkPlayerResponse(playerSelection);
      return playerSelection;
    }
  
    let playerSelection = getPlayerSelection;
  
  //emulates a single round of rock paper scissor
    function singleRound(){
      if (playerSelection === "Rock" &amp;&amp; computerSelection === "Scissors"){
        alert('You have won this round. The computer chose ' + computerSelection + '.');
        playerPoints++;
        round++;
      } else if (playerSelection === "Scissors" &amp;&amp; computerSelection === "Paper"){
        alert('You have won this round. The computer chose ' + computerSelection + '.');
        playerPoints++;
        round++
      } else if (playerSelection === "Paper" &amp;&amp; computerSelection === "Rock"){
        alert('You have won this round. The computer chose ' + computerSelection + '.');
        playerPoints++;
        round++
      } else if (playerSelection === computerSelection){
        alert('You have tied this round. The computer chose ' + computerSelection + '.');
        totalTies++;
      } else {
        alert('Something went wrong. Refresh the page.')
      }
    }
  
    function updateScore() {
      document.getElementById("player-points").innerHTML = playerPoints;
      document.getElementById("round").innerHTML = round;
      document.getElementById("computer-points").innerHTML = computerPoints;
      document.getElementById("total-ties").innerHTML = totalTies;
    }
  
  //keeps track of player points and ends when a player gets 5 points
   function game() {
      for (round = 0; computerPoints &lt; 5 &amp;&amp; playerPoints &lt; 5; round) {
        getPlayerSelection();
        computerPlay();
        singleRound();
        updateScore();
        }
    }
  
  game();</code></pre>
  </div>
  </div>
  </p>