title: How to generate a random number on page load?
body: <p>I'm trying to generate a random number when the page loads, but it doesn't seem to work for some reason. The random number just keeps on being generated every time the user presses the submit button and not only once from the beginning of the page load. </p>
  
  <p>My HTML:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
   &lt;html&gt;
    &lt;head&gt;
     &lt;title&gt; How many fingers &lt;/title&gt;
     &lt;script type="text/javascript" src="main.js"&gt; &lt;/script&gt;
   &lt;/head&gt;
  
    &lt;body&gt;
     &lt;h1&gt; Test your luck &lt;/h1&gt;
     &lt;h2&gt; How many fingers do I have?&lt;h2&gt;
     &lt;input type="text" id="myGuess"&gt;
     &lt;button id="button" onclick="check()"&gt; submit &lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My JavaScript:</p>
  
  <pre class="lang-js prettyprint-override"><code>function loadNumber(){
     var myNumber = (Math.floor(Math.random()*10) +1);
   }
   //am not sure if the above function should generate the random number!
   window.onload = loadNumber;
  
  function check(){
  
    var numberOfGuesses = (Math.floor(Math.random()*9)+1);
    var guess = document.getElementById("myGuess").value;
    var number = (Math.floor(Math.random()*10) +1);
  
    if(guess==number){
        alert("correct, it took you " + numberOfGuesses + " guesses to get it 
         right")
        document.location.reload();
     }else{
      numberOfGuesses++;
      alert("nope, try again!")
    }
  }
  </code></pre>