Fix Random.nextLong returning value out of range KT-47304

Correctly zero-extend the lower Int part of the Long being produced
This commit is contained in:
Ilya Gorbunov
2021-09-29 06:50:41 +00:00
committed by Space
parent 43f64bc7b4
commit 193effc7c4
2 changed files with 10 additions and 8 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -135,7 +135,7 @@ public abstract class Random {
nextInt().toLong() and 0xFFFF_FFFF
else -> {
val bitCount = fastLog2(nHigh)
nextBits(bitCount).toLong().shl(32) + nextInt()
nextBits(bitCount).toLong().shl(32) + (nextInt().toLong() and 0xFFFF_FFFF)
}
}
} else {