-
[코틀린] 가중치 랜덤카테고리 없음 2024. 4. 19. 21:11
Map에서 key는 제네릭, value는 Double로 받아서
가중치를 기반으로 key를 뽑는 구문이다.
import kotlin.math.ln import kotlin.random.Random private val random = Random fun <T> Map<T, Double>.random(): T { val entry = entries.minByOrNull { -ln(random.nextDouble()) / it.value } return entry?.key ?: throw IllegalArgumentException() }