title: Why does the output of the function 0 even though 0 isn't in the range?
body: <p>I am a python developer learning javascript and NODE.js. I was practicing some javascript and one of the things I miss in python is the random.randint() function. I have tried to code my own randint function using Math.random(). The function is working great but the problem is that sometimes it outputs a value that isn't within the range given.</p>
  
  <pre><code>function true_round(num){
  
      if (Number.isInteger(num)) { return num; }
  
      let [before, after] = String(num).split(".");
  
      if (Number(after.slice(0, 1)) &gt;= 5) { return Number(before) + 1; }
  
      else { return Number(before); }
  
  }
  
  
  function randint(lowerbound, upperbound){
  
      if (upperbound &gt; 0){ 
  
          let rand = lowerbound + true_round(upperbound * Math.random());
  
          if (rand &gt; upperbound){ return upperbound; }
  
          return rand;
  
      }
  
      else if (upperbound &lt;= 0){
  
          let rand = upperbound + true_round(lowerbound * Math.random())
  
          if (rand &lt; lowerbound){ return lowerbound; }
  
          return rand;
  
      }
  }
  
  for (i = 0; i &lt; 100000; i++){ console.log(randint(-2, -1)); }
  </code></pre>
  
  <p>I expect all the outputs to be within the range given but it gives me 0 sometimes.</p>