title: Why does scala return an out of range value in this modulo operation?
body: <p>This is a piece of code to generate random Long values within a given range, simplified for clarity:</p>
  
  <pre><code>def getLong(min: Long, max: Long): Long = {
    if(min &gt; max) {
      throw new IncorrectBoundsException
    }
    val rangeSize = (max - min + 1L)
    val randValue = math.abs(Random.nextLong())
    val result = (randValue % (rangeSize)) + min
    result
  }
  </code></pre>
  
  <p>I know the results of this aren't uniform and this wouldn't work correctly for some values of <code>min</code> and <code>max</code>, but that's beside the point.</p>
  
  <p>In the tests it turned out, that the following assertion isn't always true:</p>
  
  <pre><code>getLong(-1L, 1L) &gt;= -1L
  </code></pre>
  
  <p>More specifically the returned value is <code>-3</code>. How is that even possible?</p>