kozalosev / DickGrowerBot

Who has the biggest dick ever? A game bot for Telegram
https://t.me/DickGrowerBot
Other
8 stars 2 forks source link

Fairness of randomness #33

Closed kozalosev closed 3 weeks ago

kozalosev commented 3 months ago

Recently, I have received several messages concerning about possible unfairness of the PvP battles in the bot. I decided to describe this arguable and discussable issue once and refer them all here.

Some participants of the game allegedly have a win rate of about 80-90%. I understand this may look suspicious, but this is not sufficient to conclude the randomness is not random. To make some conclusions, we must take into consideration a total number of battles conducted by the user. In probability theory, results are uniformly distributed only when the number of attempts tends to infinity. In real life, we have to deal with hundreds or thousands of attempts. True randomness on a small sample can seem very non-random for humans. It's normal. It doesn't mean something is broken. Even more, to better understand what's happening, it's desirable to collect statistics of battles for all the users. Unfortunately, I haven't implemented such functionality yet, sorry.

Implementation details

The code to choose a winner is very simple:

fn choose_winner<T>(initiator: T, acceptor: T) -> (T, T) {
    if OsRng.gen_bool(0.5) {
        (acceptor, initiator)
    } else {
        (initiator, acceptor)
    }
}

OsRng uses the getrandom() system call to get a truly random value from physical sensors on a physical machine, and applies a Bernoulli distribution over it to get a boolean value: Bernoulli constructor docstring

However, I think there may be a problem: the bot is running on a virtual machine which has a shared pool of randomness/chaos. Theoretically, it means that other applications on the same machine can use that pool, and it can be constantly empty. But this hypothesis requires further investigation and addition of corresponding metrics.

Tasks

Taking into account the said above, let's formulate a list of tasks to do to prove or disprove the fairness of the randomness:

Other ideas?

If someone else has other thoughts, I would be glad to listen to them here.

kozalosev commented 3 months ago

https://unix.stackexchange.com/questions/94206/how-to-determine-which-processes-are-using-how-much-entropy-from-dev-urandom

The kernel does have an estimate on the amount of entropy available (readable from /proc/sys/kernel/random/entropy_avail)

$ cat /proc/sys/kernel/random/entropy_avail                                                                                                                                                                                                                                    
256

Everything's fine with the entropy pool.

I discussed this topic with more experienced in gambling colleagues yesterday. Mathematically and in implementation, everything must be fine. We don't need another Rng engine, nor any metrics.

kozalosev commented 3 months ago

https://github.com/kozalosev/DickGrowerBot/pull/35

This is not the log I said above, but at least now we'll have some statistics.

divadsn commented 2 months ago

Just to chime in, we recently did some PvP battles in a group chat and nearly everyone lost in multiple bets against a friend:

image

Some of the top 10 did not participate in battles or only lost a bit, but the randomness seems odd to me. I think maybe the size of both sides and the value they are betting on should be factored into the odds to balance things out.