06 July 2012 | | categories: orms, projects, r package, rstats, school, software | blog archive
-[ background ]-
I took a course in graduate school (“Statistical Analysis for Digital Simulation”) at Texas Tech from Dr. Elliot Montes in which we spent a lot of time on the generation and analysis of random numbers. Here is a simple-yet-powerful (pseudo)random number generator that I learned about in that course. It is named “The MLS Junk Generator” after Dr. Milton L. Smith. I thought of it recently [07/2012] (not sure why) and decided to share.
[Update 02/2015] Now that I’m learning R, I decided to convert the generator to that language and clean-up and post the Excel/VBA versions I already had.
[Update 08/2015] This is now available as an R package (CRAN, GitHub).
-[ the mls junk generator ]-
For any seed values of w, x, y, z:
r_{i} = 5.980217w^{2} + 9.446377x^{0.25} + 4.81379y^{0.33} + 8.91197z^{0.5}
r_{i} = r_{i} - Int(r_{i})
For r_{i+1}:
w = x
x = y
y = z
z = r_{i}
-[ analysis ]-
This generator tends to do well with various tests for randomness (K-S, Chi Square, test for runs up and down). It may not perform as well on other tests (e.g., tests for runs above and below the mean), but that could relate to my choice of seeds. As a point of reference, the period of Excel’s built-in random number generator is 16,777,216 (at least in Excel 2003; the algorithm may have changed in Excel 2007 or 2010 [Edit: this seems to still be the period of Excel’s RNG.]) and the MLS Junk Generator’s period is something greater than 9.9 billion (I gave up on trying to calculate it when it reached that point).
-[ vba code ]-
Here is an example of VBA code that generates a stream of random numbers in column A in Excel, assuming user-defined values of the parameters w, x, y, z, _and _n (the number of random numbers to generate) in cells C1 to C5. This can be downloaded in .xlsm or .bas format (see below).
Option Explicit
Public Sub MLS_Junk_Generator()
Dim r, ri, w, x, y, z As Single
Dim i, n As Integer
Application.ScreenUpdating = False
n = Cells(1, 3).Value
w = Cells(2, 3).Value
x = Cells(3, 3).Value
y = Cells(4, 3).Value
z = Cells(5, 3).Value
Cells(1, 1).Value = "MLS Junk Generator Stream"
For i = 1 To n
r = 5.980217 * (w^2) + 9.446377 * (x^0.25) + 4.81379 * (y^0.33) + 8.91197 * (z^0.5)
ri = r - Int(r)
Cells(i + 1, 1).Value = Format(ri, "0.0000")
w = x
x = y
y = z
z = ri
Next i
Application.ScreenUpdating = True
End Sub
-[ r code (aka MLS Junk GeneratR) ]-
In February 2015, I wrote two R functions with the same user-defined parameters as the VBA code above. The file (see below) also contains a version of this function that generates a data frame.
-[ r package (mlsjunkgen) ]-
Installation:
install.packages("mlsjunkgen")
library(mlsjunkgen)
install.packages("devtools")
library(devtools)
install_github("scumdogsteev/mlsjunkgen")
library(mlsjunkgen)
Functions
The package consists of four functions:
Examples:
junkgen generates a single pseudo-random number based on four user-specified seeds:
w <- 1
x <- 2
y <- 3
z <- 4
junkgen(w = w, x = x, y = y, z = z)
#> [1] 0.9551644
mlsjunkgenv generates a vector containing a stream of n (default = 1) user-specified pseudo-random numbers based on four user-specified seeds rounded to a specified (default = 5) number of decimal places:
mlsjunkgenv(n = 10, w = w, x = x, y = y, z = z, round = 8)
#> [1] 0.9551644 0.6690774 0.2123540 0.3448801 0.1199463 0.5639798 0.5923515
#> [8] 0.1143156 0.3352500 0.7027079
## The same example with default rounding:
mlsjunkgenv(n = 10, w = w, x = x, y = y, z = z)
#> [1] 0.95516 0.66908 0.21235 0.34488 0.11995 0.56398 0.59235 0.11432
#> [9] 0.33525 0.70271
mlsjunkgend generates a data frame containing a stream of n user-specified pseudo-random numbers based on four user-specified seeds:
mlsjunkgend(n = 10, w = w, x = x, y = y, z = z, round = 8)
#> RN
#> 1 0.9551644
#> 2 0.6690774
#> 3 0.2123540
#> 4 0.3448801
#> 5 0.1199463
#> 6 0.5639798
#> 7 0.5923515
#> 8 0.1143156
#> 9 0.3352500
#> 10 0.7027079
## The same example with default rounding:
mlsjunkgend(n = 10, w = w, x = x, y = y, z = z)
#> RN
#> 1 0.95516
#> 2 0.66908
#> 3 0.21235
#> 4 0.34489
#> 5 0.11995
#> 6 0.56398
#> 7 0.59235
#> 8 0.11432
#> 9 0.33525
#> 10 0.70271
mlsjunkgenm generates a matrix of user-specified size containing a stream of random numbers based on four user-specified seeds:
mlsjunkgenm(nrow = 5, ncol = 5, w = w, x = x, y = y, z = z, round = 3)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.955 0.564 0.418 0.052 0.020
#> [2,] 0.669 0.592 0.313 0.663 0.110
#> [3,] 0.212 0.114 0.920 0.802 0.685
#> [4,] 0.345 0.335 0.379 0.160 0.286
#> [5,] 0.120 0.703 0.280 0.586 0.452
Versions of mlsjunkgen will be named after things related to Texas Tech University.
-[ license ]-
-[ project changelog ]-
2021-04-19
2015-09-07
2015-08-16
2015-02-01
2015-01-31
2012-07-06
Spring 2004
-[ download excel/vba implementation ]-
-[ download original r implementation ]-
-[ download/install r package ]-