public
Created

Pseudo-Random vs. Random Numbers in R

  • Download Gist
gistfile1.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
library('plyr')
library('pixmap')
library('random')
 
rand_bit_matrix <- function(num_rows = 500, num_cols = 500,
max_n_random.org = 10000, seed = NULL) {
# I have copied the following function directly from help("integer").
is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) {
abs(x - round(x)) < tol
}
# The number of bits to draw at "random".
n <- num_rows * num_cols
if(n <= 0 || !is.wholenumber(n)) {
stop("The number of bits 'n' should be a natural number.")
}
 
if(!is.null(seed)) {
set.seed(seed)
}
 
# Create a matrix of pseudo-random bits.
bits_R <- replicate(n = num_cols, sample(c(0, 1), size = num_rows, replace = TRUE))
# Because random.org will only return a maximum of 10,000 numbers at a time,
# we break this up into several calls.
seq_n_random.org <- rep.int(x = max_n_random.org, times = n %/% max_n_random.org)
if(n %% max_n_random.org > 0) {
seq_n_random.org <- c(seq_n_random.org, n %% max_n_random.org)
}
bits_random.org <- lapply(seq_n_random.org, function(n) {
try_default(randomNumbers(n = n, min = 0, max = 1, col = 1), NA)
})
bits_random.org <- matrix(unlist(bits_random.org), nrow = num_rows, ncol = num_cols)
list(R = bits_R, random.org = bits_random.org)
}
 
bit_mats <- rand_bit_matrix(num_rows = 500, num_cols = 500, seed = 42)
 
with(bit_mats,
plot(pixmapGrey(data = R, nrow = nrow(R), ncol = ncol(R)), main = "R")
)
with(bit_mats,
plot(pixmapGrey(data = random.org, nrow = nrow(random.org), ncol = ncol(random.org)), main = "random.org")
)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.