All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.databob.CoinToss.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
package io.github.databob

import io.github.databob.Generators
import java.util.*

/**
 * Represents a weighted binary choice in data generation
 */
class CoinToss(private val successRate: Int) {

    /**
     * Pre-packed generator instances
     */
    object generators {
        val even = Generators.ofType { -> CoinToss(50) }
        val alwaysTails = Generators.ofType { -> CoinToss(0) }
        val alwaysHeads = Generators.ofType { -> CoinToss(100) }
    }

    init {
        if (successRate < 0 || successRate > 100) throw IllegalArgumentException("Success rate % must be 0-100")
    }

    fun toss(): Boolean = Random().nextInt(100) < successRate
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy