fix sign in code to generate a random signed short (#402)

* fix sign in code to generate a random signed short

* Update IdGenerator.scala

* Update IdGenerator.scala
This commit is contained in:
PJ Fanning 2023-06-16 09:55:09 +01:00 committed by GitHub
parent ead8c7e889
commit 1281083b32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,7 +30,9 @@ private[pekko] trait IdGenerator {
*/
@InternalApi
private[pekko] object IdGenerator {
private val MaxUnsignedShort = 65535
// Random.nextInt(bound) generates a random int in the range 0 (inclusive) to bound (exclusive),
// so add 1 to Max Unsigned Short (65535)
private val UnsignedShortBound = 65536
sealed trait Policy
@ -57,5 +59,5 @@ private[pekko] object IdGenerator {
* @return a random sequence of ids for production
*/
def random(rand: java.util.Random): IdGenerator =
() => (rand.nextInt(MaxUnsignedShort) - Short.MinValue).toShort
() => (rand.nextInt(UnsignedShortBound) + Short.MinValue).toShort
}