Modulo bias: rand() % n
Programmers often write rand() % n to compress a random number into a desired range. This is a dangerous habit: if the number of values of the generator is not divisible without a remainder by n, some results will appear more often than others. This phenomenon is called modulo bias.
The reason is simple: if we divide 16 values by 6, 16 = 2·6 + 4. This means there are at least 2 values for each result, but the remaining 4 values are only added to some results. As a result, the wheel "tilts to the right".
An online casino's wheel of fortune is written like this:
def tasodifiy():
"""Apparat generatori: 0..15 oraligʻida bir tekis son qaytaradi."""
return raw_random() & 0x0F # 16 ta teng ehtimolli qiymat
def gildirak():
return tasodifiy() % 6 # 0..5 — g'ildirak sektori
The attached file contains 30000 real results of the game: each is a single number (0..5), written on separate lines.
Count the results and determine the two sectors that appear the least. Assemble the flag from these two numbers in ascending order: BAYROQ{PRNG_bias_kam_<kichigi>_<kattasi>}
There is also a theoretical way: manually write down how many times the remainder occurs when dividing values from 0..15 by 6.