Any Mersenne Twister algorithm experts out there? Based on some experiments, I suspect that the longest initial run of identical values, that is all 0s or all 1s, from rbinom in R is 32. Since that’s a power of 2, I wonder whether it’s an artifact of the algorithm or implementation. Does anyone know?
Edited (9 Nov 2025) to add: one working hypothesis is that the number of initial runs is driven by the seed space (\(2^{32}\) integers) and the probability of runs over that – so constrained by probability rather than the algorithm. Jan van der Laan (@dodecadron@datasci.social) looked for runs in a long vector of rbinom draws on a fixed seed and found one length 33 further along the sequence (somewhere).
set.seed(182) > x <- rbinom(1e8, 1, 0.5) r <- rle(x) table(r$lengths)
If you take 30 draws from a Bernoulli distribution, e.g., to randomise people to one of two conditions, there’s a \(\frac{2}{2^{30}} = \frac{1}{2^{29}} = \frac{1}{536,870,912}\) chance that all 30 will be assigned to the same condition (all 1s or all 0s).
That’s vanishingly unlikely, but using the following seed with R’s rbinom produces 30 ones:
Unlikely but possible – even if your random numbers come from atmospheric noise or quantum measurements rather than a pseudorandom generator. If you want to rule out this outcome entirely (make the probability zero), then you don’t actually want independent Bernoulli draws. You’d need a different randomisation scheme, such as blocked randomisation.
I often see reports describing checks on random sequences after they’ve been generated, when the researchers could have saved themselves the effort by building in the kind of randomness they wanted from the start.
P.S. The longest initial runs I know about are for seed 796110494, which yields 31 zeros, and 1002755089, which yields 32 ones.
Suppose you have a coin that you suspect might be biased. Here’s how you can debias it so that there’s a 50-50 chance of heads or tails, thanks to a neat idea often attributed to John von Neumann (1951/1963, p. 768):
“… in tossing a coin it is probably easier to make two consecutive tosses independent than to toss heads with probability exactly one-half. If independence of successive tosses is assumed, we can reconstruct a 50-50 chance out of even a badly biased coin by tossing twice. If we get heads-heads or tails-tails, we reject the tosses and try again. If we get heads-tails (or tails-heads), we accept the result as heads (or tails). The resulting process is rigorously unbiased, although the amended process is at most 25 percent as efficient as ordinary coin-tossing.”
Why does this work? First, the probability of heads followed by tails (\(HT\)) is the following product, since coin flips are independent:
\(P(HT) = P(H) [1-P(H)]\)
We get the same answer for tails followed by heads (\(TH\)):
\(P(TH) = [1-P(H)] P(H) = P(H) [1-P(H)]\)
So, \(P(HT) = P(TH)\), which already hints at why this works.
For example, if a coin is so ridiculously biased that it only has a 10% chance of a heads outcome, then
We really want a debiased coin to have a 50-50 chance of a heads or tails outcome. That’s where ignoring \(HH\) and \(TT\) outcomes helps: we condition on \(HT\) or \(TH\).
Random numbers are important for lots of things including statistical analysis (e.g., using bootstrapping) and cryptography (e.g., producing a one-time pad). But computers can’t produce random numbers. They produce pseudorandom numbers, which are deterministic and hence predictable given the random seed.
Pseudorandom numbers more than suffice for most purposes (so long as everyone isn’t using the same seeds like 1234 or 42); however, if you want or need the real thing there are various ways to pipe true randomness into computers. Two are available via R packages.
The best known is {random}, which exports pure randomness from Ireland. The basic idea is that a radio receiver is tuned to a frequency where no station broadcasts. This is continuously recorded and sampled to generate random numbers.
Another is {qrandom} (no longer on CRAN), provided by the Australian National University. This works by measuring the quantum fluctuations of the vacuum.