I felt inspired to do 10 PRINT CHR$(205.5+RND(1)); : GOTO 10 in R.
knitr::opts_chunk$set(
collapse = TRUE, comment = "#>", fig.width = 5, fig.height = 5)
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
draw_print10 <- function(height = 100, width = 100, rows = 20,
cols = 20, p = .5) {
x_starts <- seq(0, height, length.out = rows + 1)
x_ends <- seq(0, height, length.out = rows + 1)
x_starts <- x_starts[-length(x_starts)]
x_ends <- x_ends[-1]
y_starts <- seq(0, width, length.out = cols + 1)
y_ends <- seq(0, width, length.out = cols + 1)
y_starts <- y_starts[-length(y_starts)]
y_ends <- y_ends[-1]
grid <- tidyr::crossing(
tidyr::nesting(x_start = x_starts, x_end = x_ends),
tidyr::nesting(y_start = y_starts, y_end = y_ends))
grid <- grid %>%
mutate(
flip = runif(length(y_end)) <= p,
y_temp1 = y_start,
y_temp2 = y_end,
y_start = ifelse(flip, y_temp2, y_temp1),
y_end = ifelse(flip, y_temp1, y_temp2)) %>%
select(x_start:y_end)
ggplot(grid) +
aes(x = x_start, y = y_start, xend = x_end, yend = y_end) +
geom_segment(size = 1)
}
# Equal chance of flipping
draw_print10() +
theme_void()
# Much lower chance
draw_print10(p = .10) +
theme_void()
# Vectorized probabilities so that p(/) changes with x value
p <- seq(0, 1, length.out = 20 * 20)
draw_print10(p = p) +
theme_void()
# Crank up the resolution
p <- seq(0, 1, length.out = 40 * 40)
draw_print10(rows = 40, cols = 40, p = p) +
theme_void()
draw_print10(rows = 40, cols = 40, p = p) +
theme_void() +
aes(color = p) +
guides(color = FALSE)
# Use Gaussian densities as probabilities
p <- dnorm(seq(-4, 4, length.out = 40 * 40))
draw_print10(rows = 40, cols = 40, p = p) +
theme_void() +
aes(color = p) +
guides(color = FALSE)
# Use Cauchy densities as probabilities (fatter tail)
p <- dcauchy(seq(-4, 4, length.out = 40 * 40))
draw_print10(rows = 40, cols = 40, p = p) +
theme_void() +
aes(color = p) +
guides(color = FALSE)
# Use flipped exponential densities as probabilities
p <- c(dexp(seq(0, 4, length.out = 40 * 20)),
dexp(seq(4, 0, length.out = 40 * 20)))
draw_print10(rows = 40, cols = 40, p = p) +
theme_void() +
aes(color = p) +
guides(color = FALSE)