一个泛型方法,可以返回两个参数之间的随机整数,就像ruby对rand(0..n)所做的那样。
任何建议吗?
一个泛型方法,可以返回两个参数之间的随机整数,就像ruby对rand(0..n)所做的那样。
任何建议吗?
当前回答
在Kotlin SDK >=1.3中,你可以这样做
import kotlin.random.Random
val number = Random.nextInt(limit)
其他回答
基于@s1m0nw1的优秀答案,我做了以下更改。
(0..n)在Kotlin中表示包含 (0到n)在Kotlin中表示独占 为Random实例使用单例对象(可选)
代码:
private object RandomRangeSingleton : Random()
fun ClosedRange<Int>.random() = RandomRangeSingleton.nextInt((endInclusive + 1) - start) + start
测试用例:
fun testRandom() {
Assert.assertTrue(
(0..1000).all {
(0..5).contains((0..5).random())
}
)
Assert.assertTrue(
(0..1000).all {
(0..4).contains((0 until 5).random())
}
)
Assert.assertFalse(
(0..1000).all {
(0..4).contains((0..5).random())
}
)
}
没有标准的方法可以做到这一点,但是您可以使用Math.random()或java.util.Random类轻松创建自己的方法。下面是一个使用Math.random()方法的例子:
fun random(n: Int) = (Math.random() * n).toInt()
fun random(from: Int, to: Int) = (Math.random() * (to - from) + from).toInt()
fun random(pair: Pair<Int, Int>) = random(pair.first, pair.second)
fun main(args: Array<String>) {
val n = 10
val rand1 = random(n)
val rand2 = random(5, n)
val rand3 = random(5 to n)
println(List(10) { random(n) })
println(List(10) { random(5 to n) })
}
这是一个示例输出:
[9, 8, 1, 7, 5, 6, 9, 8, 1, 9]
[5, 8, 9, 7, 6, 6, 8, 6, 7, 9]
Kotlin标准库不提供随机数生成器API。如果你不是在一个多平台的项目中,最好使用平台api(这个问题的所有其他答案都在谈论这个解决方案)。
但是如果您处于多平台环境中,最好的解决方案是自己在纯kotlin中实现random,以便在平台之间共享相同的随机数生成器。这对于开发和测试来说更加简单。
为了在我的个人项目中解决这个问题,我实现了一个纯Kotlin线性同余生成器。LCG是java.util.Random使用的算法。如果你想使用它,请点击以下链接: https://gist.github.com/11e5ddb567786af0ed1ae4d7f57441d4
我的实现目的nextInt(范围:IntRange)为您;)。
注意我的目的,LCG适用于大多数用例(模拟,游戏等),但不适合密码学使用,因为这种方法的可预测性。
你可以创建一个扩展函数:
infix fun ClosedRange<Float>.step(step: Float): Iterable<Float> {
require(start.isFinite())
require(endInclusive.isFinite())
require(step.round() > 0.0) { "Step must be positive, was: $step." }
require(start != endInclusive) { "Start and endInclusive must not be the same"}
if (endInclusive > start) {
return generateSequence(start) { previous ->
if (previous == Float.POSITIVE_INFINITY) return@generateSequence null
val next = previous + step.round()
if (next > endInclusive) null else next.round()
}.asIterable()
}
return generateSequence(start) { previous ->
if (previous == Float.NEGATIVE_INFINITY) return@generateSequence null
val next = previous - step.round()
if (next < endInclusive) null else next.round()
}.asIterable()
}
Round Float值:
fun Float.round(decimals: Int = DIGITS): Float {
var multiplier = 1.0f
repeat(decimals) { multiplier *= 10 }
return round(this * multiplier) / multiplier
}
方法的用法:
(0.0f .. 1.0f).step(.1f).forEach { System.out.println("value: $it") }
输出:
取值:0.0值:0.1值:0.2值:0.3值:0.4值:0.5 取值:0.6值:0.7值:0.8值:0.9值:1.0
你可以创建一个类似于java.util.Random.nextInt(int)的扩展函数,但它的边界是一个inrange而不是int:
fun Random.nextInt(range: IntRange): Int {
return range.start + nextInt(range.last - range.start)
}
你现在可以对任何随机实例使用这个:
val random = Random()
println(random.nextInt(5..9)) // prints 5, 6, 7, 8, or 9
如果你不想管理自己的随机实例,那么你可以定义一个方便的方法,例如使用ThreadLocalRandom.current():
fun rand(range: IntRange): Int {
return ThreadLocalRandom.current().nextInt(range)
}
现在你可以像在Ruby中一样获得一个随机整数,而不需要自己首先声明一个随机实例:
rand(5..9) // returns 5, 6, 7, 8, or 9