Kotlin Random.Default用法及代码示例

本文方法及代码示例基于Kotlin 2.1.20 Released

Random.Default 所在包 kotlin.random.Random.Default,其相关用法介绍如下:

用法:

kotlin 复制代码
companion object Default : Random, Serializable

默认随机数生成器。

在 JVM 上,这个生成器是线程安全的,它的方法可以从多个线程中调用。

代码示例:

kotlin 复制代码
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue

fun main(args: Array<String>) {
    //sampleStart
    val randomValues = List(10) { Random.nextInt(0, 100) }
    // prints new sequence every time
    println(randomValues)

    val nextValues = List(10) { Random.nextInt(0, 100) }
    println(nextValues)
    println("randomValues != nextValues is ${randomValues != nextValues}") // true
    //sampleEnd
}

// 输出
[63, 94, 85, 15, 93, 45, 19, 57, 39, 48]
[9, 15, 79, 3, 74, 61, 64, 78, 67, 22]
randomValues != nextValues is true

相关方法

相关推荐
Kapaseker1 小时前
Kotlin 又要加语法糖了:这次轮到 if-else
android·kotlin
恋猫de小郭3 小时前
Flutter 3.50 & Dart 3.14 发布,快来看看有什么更新吧
android·前端·flutter
Starry-sky(jing)21 小时前
Vidda ADB 开启:R Pro 65 找不到开发者选项的处理与连接验收
android·adb·远程控制·电视·vidda
雨白21 小时前
从原理到实践:使用 Kotlin KSP 仿写 ButterKnifeLite
android·kotlin
疯华整猫1 天前
把二十七个 init 从首帧前面搬走:用 Trae 把启动优化钉进 CI
android·性能优化·ai编程
灿飞1 天前
Kotlin与Java虚拟机十一:类与属性:构造顺序、backing field 与显式 backing field
java·后端·kotlin
传奇开心果1 天前
【探秘 Jetpack Compose 框架深水区】 第8课:rememberSaveable 的边界在哪里?——Bundle、Saver 与什么值得跨进程存活
android·kotlin
Frankie_stand1 天前
测试自动化实战指南:从脚本化、自动判据到 CI 集成的完整落地路径
android·ci/cd·自动化