Skip to content

Instantly share code, notes, and snippets.

@djnavarro
Created December 27, 2019 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djnavarro/c992f93681f9b13141101d8a802ff53c to your computer and use it in GitHub Desktop.
Save djnavarro/c992f93681f9b13141101d8a802ff53c to your computer and use it in GitHub Desktop.
The joy of coding when the RNG state is global...
# someone else's package includes this inefficient function
pick_name <- function() {
names <- c("Dax", "Wug", "Lep", "Sik", "Bop")
sample(names)[1]
}
# here is my code that calls it...
set.seed(100)
print("--- Attempt 1 ---")
print(pick_name()) # Wug
print(pick_name()) # Dax
print(pick_name()) # Sik
# it is reproducible...
set.seed(100)
print("--- Attempt 2 ---")
print(pick_name()) # Wug
print(pick_name()) # Dax
print(pick_name()) # Sik
# now the developer pushes a new and improved version
pick_name <- function() {
names <- c("Dax", "Wug", "Lep", "Sik", "Bop")
sample(names, size = 1)
}
# my output is no longer reproducible...
set.seed(100)
print("--- Attempt 3 ---")
print(pick_name()) # Wug
print(pick_name()) # Lep
print(pick_name()) # Dax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment