24 lines
577 B
Kotlin
24 lines
577 B
Kotlin
package com.infendro.random.prng
|
|
|
|
import com.infendro.bytes.bytearray.toLong
|
|
import com.infendro.hash.sha2.`SHA-256`
|
|
import com.infendro.random.PRNG
|
|
import kotlin.properties.Delegates.notNull
|
|
|
|
class LCG : PRNG() {
|
|
private val a: Long = 25214903917L
|
|
private val c: Long = 11L
|
|
private val mask: Long = (1L shl 48) - 1
|
|
|
|
private var x: Long by notNull()
|
|
|
|
override fun seed(seed: ByteArray) {
|
|
x = `SHA-256`.hash(seed).toLong() and mask
|
|
}
|
|
|
|
override fun generate(): Int {
|
|
x = (a * x + c) and mask
|
|
return (x ushr 16).toInt()
|
|
}
|
|
}
|