nextBits

Common
JVM
JS
Native
1.0
fun nextBits(bitCount: Int): Int
(source)

Gets the next random bitCount number of bits.

Generates an Int whose lower bitCount bits are filled with random values and the remaining upper bits are zero.

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

fun main(args: Array<String>) {
//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
}

Parameters

bitCount - number of bits to generate, must be in range 0..32, otherwise the behavior is unspecified.