2026-09-16

What is Statistical Randomness?

  • A process is “random” if it is unpredictable.
  • A numeric sequence is said to be statistically random when it contains no recognizable patterns or regularities.
  • Statistical randomness does not imply true randomness.
  • We use pseudorandomness instead, produced through deterministic means.
  • Use is for “random enough” tests with data.

Random Number Generation

  • Generate sequences of numbers, hopefully seeming random.
  • Focusing on two today: Mersenne Twister & Middle-Square Method.
  • Mersenne Twister is integrated into R already.
  • Middle-Square Method will be coded.
  • Seed value: our starting point for generation.
  • Using 6356 as seed.

Mersenne Twister

  • Pseudorandom number generator, meaning it is deterministic.
  • Developed 1997 by Makoto Matsumoto and Takuji Nishimura.
  • Based on Mersenne prime \(2^{19937} - 1\).
  • As Mersenne Twister is built into R, generating sequence is simple:
# First we set the seed
set.seed(seed)

# Then we use sample to create our sequence
mtVec = sample(0:9, 6000, replace=T)

Middle Square Method

  • Invented by John von Neumann.
  • Seed/input number needs to be even and at least 2 digits in length.
  • Sqaure starting value, add leading zeros to left to bring to 2n digits, take n digits from middle.
# 4 digit gen
middleSquare = function(seed, genCount) {
  num = seed
  genList = list()
  for (x in 1:genCount) {
    num = substr(fillZero(as.character(num*num)), 3, 6)
    genList = splitAppend(genList, num)
    num = as.numeric(num)
  }
  return(unlist(genList))
}

Testing Randomness

  • Used to analyze distribution of data to see if can be described as random.
  • Generation is deterministic, so it will never be “true random.”
  • If data is random enough, we can use it for simulation runs etc.
  • Using a couple tests: Frequency Test, Serial Test, and 3d Plot Test
  • Looking for patterns, not just testing null hypotheses.
  • 0000011111 is not random, but just testing based on frequency wouldn’t show that.
  • Using Pearson’s chi-squared test for mathematical testing.

Frequency Test - Mersenne Twister

  • With 10 numbers (0-9), we would expect each number to occur one in ten times. Some variation occurs, but nothing notable.

Cont. (Chi-Square Test)

Our sample size is 6000, with 10 possible outcomes. \(\frac{6000}{10} = 600\) - we expect roughly 600 of each number to occur, which is our null hypothesis. The equation we will use is: \(\sum_{i = 1}^{10} \frac{(O_{i} - 600)^{2}}{600}\), where \(O_{i}\) is the frequency count of our sequence, in this case \(O_{i} = \langle 601, 583, 589, 623, 572, 606, 582, 630, 618, 596\rangle\) The result of said equation is 5.54, our test statistic. With 9 degrees of freedom, assuming a p-value of 0.05, we get 16.92 as our critical value. as \(5.54 < 16.92\), we do not reject the null hypothesis.

Frequency Test: Middle-Square Method

Here we see extreme variation in the frequency of generated numbers.

Cont. Chi-Square Test

Using the same equation as for Mersenne Twister, all we have different is the frequency table. This time it is \(O_{i} = \langle 2995, 1496, 375, 1, 373, 375, 2, 376, 9\rangle\). Calculating gives us a test statistic of 13616.4, much higher than our critical value of 16.92. As \(13616.4 > 16.92\), we reject the null hypothesis, leaving us to assume that the Middle-Square Method can statistically be called random in no way according to this test.

Serial Test - Mersenne Twister

Another important consideration is occurrences of numbers in a sequence. The Serial Test looks at pairs of numbers, rather than the occurrence of each number alone.

Cont.

  • Only displaying 00 through 19.
  • Distribution has noticeable spikes and drops in the first set of 20 numbers.
  • Still not as obviously non-random as the frequency test for Middle-Square Method.
  • The frequency of “11” stands out from the others with its low value.

Serial Test - Middle-Square Method

Plot Test

By plotting, we can look for patterns in data that would otherwise seem random to mathematical tests. Points will be in (xx, yy, zz) format, made of each 6 numbers in the sequences generated. In the following plots, we will see the Mersenne Twister lacks discernible patterns, while the Middle-Square Method results in barely any variation of points within a few locales.

Consider looking up the RANDU random number generation method for an example on what can be seen with this test. This link contains a visual of such: https://imgur.com/a/randu-visualization-QfzUHe6.

Plot Test - Mersenne Twister

Plot Test - Middle-Square Method