Jump to content

Probability Based Code Execution


WrEcked

Recommended Posts

To allow for more randomisation throughout my scripts, I've implemented a how I like to call it, Lottery helper. It's a rather simple re-usable Singleton, which allows for lambda code execution, if a given probability is achieved. 

For this, I've created the following Singleton:
 

package scripts.utils.antiban

import scripts.utils.Logger
import kotlin.random.Random

/**
 * Utility class to randomize certain actions on a probability(%) basis.
 */
object Lottery {
    var logger: Logger? = null

    fun initLogger(log: Logger) {
        this.logger = log
    }

    /**
     * Pass through a probability and lambda to execute, if our case is within the probability threshold
     */
    fun execute(probability: Double, action: () -> Unit) {
        val won: Boolean = shouldExecute(probability)

        if (won) {
            this.logger?.info("[Lottery/Antiban] - Executing randomized action")
            action()
        }
    }

    /**
     * Probability refers to the rough percentage % this action will be executed on
     */
    private fun shouldExecute(probability: Double): Boolean {
        require(probability in 0.0..1.0) { "Probability must be between 0.0 and 1.0" }

        val diceRoll = Random.nextDouble()
        return diceRoll < probability
    }


}

 

For example, executing a mini-break every now and then: 

Lottery.execute(0.16) {
    MiniBreak.leave()
}

 

Or, to better demonstrate, by running a 1.000 iterations:

fun main() {
    val probabilities = listOf(0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9) // Test probabilities from 1% to 90%

    probabilities.forEach { probability ->
        var executedCount = 0
        val totalRuns = 1000 // Run each test 1000 times for a statistical sample

        repeat(totalRuns) {
            Lottery.execute(probability) {
                executedCount++ // Increment if action is executed
            }
        }

        println("Probability: ${(probability * 100).toInt()}% -> Executed: $executedCount times out of $totalRuns")
    }
}

 

Which resulted in the following:

Probability: 1% -> Executed: 9 times out of 1000
Probability: 5% -> Executed: 44 times out of 1000
Probability: 10% -> Executed: 97 times out of 1000
Probability: 25% -> Executed: 227 times out of 1000
Probability: 50% -> Executed: 523 times out of 1000
Probability: 75% -> Executed: 733 times out of 1000

 

Hope this introduction either helped you out or inspired you to create amazing, randomised code!


 

Edited by WrEcked
Added tag

image.png.35dbedeeba420b10b97824630d72deed.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...