Skip to content

Instantly share code, notes, and snippets.

@jwscook
Created July 13, 2020 10:03
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 jwscook/8df8ea24b421f23d7cc87269947d9339 to your computer and use it in GitHub Desktop.
Save jwscook/8df8ea24b421f23d7cc87269947d9339 to your computer and use it in GitHub Desktop.
using Random
const seed = hash(time())
Random.seed!(seed)
searchstring = "const seed = "
open("$(@__FILE__)" * ".reproducible", "w") do io
for line in readlines(open(@__FILE__))
if length(line) > length(searchstring) && line[1:length(searchstring)] == searchstring
write(io, searchstring * "$seed\n")
else
write(io, line * "\n")
end
end
end
@show seed
@show rand()
@jwscook
Copy link
Author

jwscook commented Jul 13, 2020

The below as Rerunnable.jl

module Rerunnable

function rerunnable(filename, replacements)
  open(filename * ".rerunnable", "w") do io
    for line in readlines(open(filename))
      for (original, replacement) in replacements
         occursin(original, line) && (line = replace(line, original => replacement))
      end
      write(io, line * "\n")
    end
  end
end

end

and then ReproduceExample.jl

using Random
include("Rerunnable.jl") # or using when/if a package
using .Rerunnable

const seed = hash(time())

Rerunnable.rerunnable(@__FILE__,  Dict("hash(time())" => "$seed"))

Random.seed!(seed)

@show seed
@show rand()

then:

$julia ReproduceExample.jl 
seed = 0x0b8683917c9ce6a1
rand() = 0.9296335898922403

$julia ReproduceExample.jl.rerunnable 
seed = 830495842180589217
rand() = 0.9296335898922403

@jwscook
Copy link
Author

jwscook commented Jul 13, 2020

And ReproduceExample.jl.rerunnable is identical to ReproduceExample.jl.rerunnable.rerunnable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment