Files
Terrarum/src/net/torvald/random/FudgeDice.kt
Song Minjae a903c629ec compressed fonts, lightmap histogram
Former-commit-id: 2fd36e0b2b7cd45ecdab628e0d2679efd17f01f3
Former-commit-id: 5c4109d7a2ce320584674c5415054a381ba4d6ec
2016-04-15 16:34:57 +09:00

48 lines
1.1 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package net.torvald.random
import java.util.Random
/**
* Created by minjaesong on 16-02-03.
*/
open class FudgeDice
/**
* Define new set of Fudge dice with given counts.
* @param randfunc java.util.Random or its extension
* *
* @param counts amount of die
*/
(private val randfunc: Random, val diceCount: Int) {
/**
* Roll dice and get result.
* @return Normally distributed integer [-N , N] for diceCount of N. 0 is the most frequent return.
*/
fun roll(): Int {
var diceResult = 0
for (c in 0..diceCount - 1) {
diceResult += rollSingleDie()
}
return diceResult
}
/**
* Roll dice and get result, for array index
* @return Normally distributed integer [0 , N] for N = 2 × DiceCounts + 1. 0 is the most frequent return.
*/
fun rollForArray(): Int {
return roll() + diceCount
}
val sizeOfProbabilityRange: Int
get() = 2 * diceCount + 1
/**
* @return integer randomly picked from {-1, 0, 1}
*/
private fun rollSingleDie(): Int {
return randfunc.nextInt(3) - 1
}
}