What is measured in this experiment is the interval between successful tests of the Bernoulli experiment, where success is defined as random() mod k == 0 for some k (36 in OP). Unfortunately, this is overshadowed by the fact that the implementation of random() means that Bernoulli's tests are not statistically independent.
We will write rnd i to output i th "random ()" and note that:
rnd i = rnd i-31 + rnd i-3 with a probability of 0.75
rnd i = rnd i-31 + rnd i-3 + 1 with a probability of 0.25
(see picture below for illustration).
Suppose rnd i-31 mod k == 0 , and we are currently looking at rnd i . Then it should be so that rnd i-3 mod k β 0 , because otherwise we would calculate the cycle as the length k-3 .
But (most of the time) (mod k): rnd i = rnd i-31 + rnd i-3 = rnd i-3 β 0 .
Thus, the current study is not statistically independent from previous trials, and the 31st trial after success will be much less successful than in the unbiased series of Bernoulli trials.
The usual advice for using linear congruent generators that does not actually apply to the random() algorithm is to use higher order bits instead of the lower bits, because the higher order bit is βmoreβ random (that is, less correlated with sequential values). But this will not work in this case either, because the above identities are equally valid for the high log k bits function as for the mod k == low log k bits function.
In fact, we could expect a linear congruent generator to work better, especially if we use high-order output bits, because although LCG is not particularly good at Monte Carlo simulations, it does not suffer from random() linear feedback.
random , for the default case:
Let state be a vector of unsigned lengths. Initialize state 0 ...state 30 using the seed, some fixed values, and a mixing algorithm. For simplicity, we can consider the state vector to be infinite, although only the last 31 values ββare used, therefore it is actually implemented as a ring buffer.
To generate rnd i : (Note: β mod 2 32 .)
state i = state i-31 β state i-3
rnd i = (state i - (state i mod 2)) / 2
Now notice that:
(i + j) mod 2 = i mod 2 + j mod 2 if i mod 2 == 0 or j mod 2 == 0
(i + j) mod 2 = i mod 2 + j mod 2 - 2 if i mod 2 == 1 and j mod 2 == 1
If i and j evenly distributed, the first case will occur in 75% of cases, and the second case - 25%.
So, by substituting into the generation formula:
rnd i = (state i-31 β state i-3 - ((state i-31 + state i-3 ) mod 2)) / 2
= ((state i-31 - (state i-31 mod 2)) β (state i-3 - (state i-3 mod 2))) / 2 or
= ((state i-31 - (state i-31 mod 2)) β (state i-3 - (state i-3 mod 2)) + 2) / 2
Two cases can be further reduced to:
rnd i = rnd i-31 β rnd i-3
rnd i = rnd i-31 & oplus; rnd i-3 + 1
As above, the first case arises in 75% of cases, considering that rnd i-31 and rnd i-3 are independently inferred from the uniform distribution (which they are not, but this is a reasonable first approximation).