一个泛型方法,可以返回两个参数之间的随机整数,就像ruby对rand(0..n)所做的那样。
任何建议吗?
一个泛型方法,可以返回两个参数之间的随机整数,就像ruby对rand(0..n)所做的那样。
任何建议吗?
当前回答
可能的变化,我的其他答案随机字符
为了获得随机字符,可以像这样定义一个扩展函数
fun ClosedRange<Char>.random(): Char =
(Random().nextInt(endInclusive.toInt() + 1 - start.toInt()) + start.toInt()).toChar()
// will return a `Char` between A and Z (incl.)
('A'..'Z').random()
如果您使用的是JDK > 1.6,请使用ThreadLocalRandom.current()而不是Random()。
对于kotlinjs和其他不允许使用java.util的用例。随机,这个答案会有帮助。
Kotlin >= 1.3多平台随机支持
在1.3版本中,Kotlin自带多平台随机生成器。在KEEP中有描述。你现在可以直接使用扩展作为Kotlin标准库的一部分,而不需要定义它:
('a'..'b').random()
其他回答
基于@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())
}
)
}
在from(含)和to(不含)之间生成一个随机整数
import java.util.Random
val random = Random()
fun rand(from: Int, to: Int) : Int {
return random.nextInt(to - from) + from
}
每当有一种情况,你想要生成密钥或mac地址,这是基于用户需求的十六进制数字,这也是使用android和kotlin,然后你我下面的代码帮助你:
private fun getRandomHexString(random: SecureRandom, numOfCharsToBePresentInTheHexString: Int): String {
val sb = StringBuilder()
while (sb.length < numOfCharsToBePresentInTheHexString) {
val randomNumber = random.nextInt()
val number = String.format("%08X", randomNumber)
sb.append(number)
}
return sb.toString()
}
你可以创建一个扩展函数:
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
我的建议是在IntRange上创建一个扩展函数,像这样创建随机(0..10).random()
TL;DR Kotlin >= 1.3,一个随机的所有平台
在1.3版本中,Kotlin自带多平台随机生成器。在KEEP中有描述。下面描述的扩展现在是Kotlin标准库的一部分,简单地像这样使用它:
val rnds = (0..10).random() // generated random from 0 to 10 included
三
在1.3之前,在JVM上我们使用Random,如果我们在JDK > 1.6上,我们甚至使用ThreadLocalRandom。
fun IntRange.random() =
Random().nextInt((endInclusive + 1) - start) + start
这样用:
// will return an `Int` between 0 and 10 (incl.)
(0..10).random()
如果你想让函数只返回1,2,…, 9(不包括10),使用until构造的范围:
(0 until 10).random()
如果您使用的是JDK > 1.6,请使用ThreadLocalRandom.current()而不是Random()。
KotlinJs和其他变体
对于kotlinjs和其他不允许使用java.util的用例。随机的,看这个选项。
另外,看看我的建议的变化。它还包括一个用于随机字符的扩展函数。