Skip to main content
Challenges Modulo bias: rand() % n
Cryptography Easy Modulo bias Solves: 6

Modulo bias: rand() % n

Author: CyberSamSU
Points
20

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.

Attached files
natijalar.txt Log in to download
SHA-256 checksums
natijalar.txt: 39189a9e7cc23bacde81fe51f106a58e119898686b867a101ab863d96b3dee7e
Hints 2
Opening a hint reduces the points you get for solving this challenge.
Hint 1 −2 pts Log in
Hint 2 −4 pts Log in
To submit a flag, log in.

First solvers

1. Лазиз 04.10.2026
2. Mirjaxon Qaxxarov 27.10.2026
3. Odam 06.11.2026
4. Doston Allayarov 12.11.2026
5. Jasmin Xuseinova 15.11.2026
6. Jumanazarov Asilbek 26.11.2026