Strange behaviour with using max
The max() “function” is actually a macro defined in Arduino.h as



#define max(a,b) ((a)&gt;(b)?(a):(b))


This expands in your code into something like

buttons_pressed = random(0, N_OF_BUTTONS) &gt; n_of_buttons_pressed) ? \
                  random(0, N_OF_BUTTONS) : n_of_buttons_pressed


The problem with the macro is that it evaluates at least one of its
arguments twice. In this case, if the test is true, the variable
buttons_pressed gets assigned another pseudo-random number.

The way around this is to store the pseudo-random number in a variable,
to avoid random() being evaluated twice:

int pseudo_random = random(0, N_OF_BUTTONS);
buttons_pressed = max(pseudo_random, n_of_buttons_pressed);