Spec-Zone.ru › Kotlin 2

nextBits

kotlin-stdlib/kotlin.random/Random/nextBits

Начиная с Kotlin: 1.3

abstract fun nextBits(bitCount: Int): Int

Получает следующее случайное число из bitCount битов.

Генерирует Int, младшие bitCount битов которого заполнены случайными значениями, а оставшиеся старшие биты равны нулю.

Параметры

bitCount

количество генерируемых битов; должно находиться в диапазоне 0..32, в противном случае поведение не определено.

Примеры

import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   // always generates a 0
println(Random.nextBits(0))
// randomly generates a 0 or 1
println(Random.nextBits(1))
// generates a random non-negative Int value less than 256
println(Random.nextBits(8))
// generates a random Int value, may generate a negative value as well
println(Random.nextBits(32)) 
   //sampleEnd
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.random/-random/next-bits.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API