Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Last active April 17, 2026 06:50
Show Gist options
  • Select an option

  • Save andrewheiss/461c749fce2783551f31611d8f0d7548 to your computer and use it in GitHub Desktop.

Select an option

Save andrewheiss/461c749fce2783551f31611d8f0d7548 to your computer and use it in GitHub Desktop.
# Monkeys, Shakespeare, typewriters, &c. ----
library(tidyverse)
# Use 9 CPUs
mirai::daemons(9)
# For computability, loop through 200,000,000 seeds
number_of_seeds <- 2e8
# Split this up into chunks of 100,000 seeds and loop through all those chunks
# in parallel with the magic of in_parallel() and {mirai}
chunk_size <- 1e5
tictoc::tic()
possible_seeds <- data.frame(seed = 1:number_of_seeds) |>
mutate(chunk = seed %/% chunk_size) |>
nest(.by = chunk) |>
mutate(
my_name = map(
data,
in_parallel(\(chunk) {
# Surprise! mirai workers don't use Mersenne Twister for their PRNG
# algorithm. They use L’Ecuyer-CMRG, which is specially designed for
# parallel processing (it seems to space out the X_n locations in the
# series for good parallelization?):
#
# https://tidyverse.org/blog/2025/09/mirai-2-5-0/#reproducible-parallel-rng
#
# So without setting the RNG kind to Mersenne Twister, this loop will
# find seeds that match HEISS, but in the L’Ecuyer-CMRG world, not the
# Mersenne Twister world.
# In this case, I'm not worried about statistically valid parallel PRNG
# series. I want to loop through the Mersenne Twister world to find
# seeds I can use in regular old set.seed() for regular old random R functions
RNGkind("Mersenne-Twister", "Inversion", "Rejection")
purrr::map_lgl(chunk$seed, \(s) {
set.seed(s)
paste0(LETTERS[sample(26, 5, replace = TRUE)], collapse = "") == "HEISS"
})
})
)
) |>
unnest(c(data, my_name))
tictoc::toc()
#> 230.042 sec elapsed
# There are 13!
name_seeds <- possible_seeds |>
filter(my_name)
name_seeds
#> # A tibble: 13 × 3
#> chunk seed my_name
#> <dbl> <int> <lgl>
#> 1 1 179449 TRUE
#> 2 370 37035397 TRUE
#> 3 429 42919191 TRUE
#> 4 620 62079621 TRUE
#> 5 626 62695475 TRUE
#> 6 764 76419452 TRUE
#> 7 790 79055727 TRUE
#> 8 834 83441964 TRUE
#> 9 936 93639412 TRUE
#> 10 964 96420576 TRUE
#> 11 1160 116071911 TRUE
#> 12 1372 137252985 TRUE
#> 13 1685 168535773 TRUE
withr::with_seed(37035397, {
paste0(LETTERS[sample(26, 5, replace = TRUE)], collapse = "")
})
#> [1] "HEISS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment