Can you generate this random sequence for me?
Inspired by this question on Retrocomputing
I want to create a random generator that always returns a desired word. I desperately need this so I can hide some easter eggs. For science easter!
Input
A word, either in string format or an array or vector of characters. You can assume all lowercase and only the regular characters a-z
.
Task
Write a full program or function that finds a seed such that when taking a random sample of letters, the desired word is output. The function then should return or save another full program or function that does the generating of this desired word.
Rules
- The returned function of program should use random sampling to create the output, so it is not allowed to hardcode (obligatory XKCD)
- The output can be written to file in the case of a full program or returned.
- Hiding an easter eggs is easiest when there is little code, so the shortest code wins.
- If there is no seed that generates the required output, your program is allowed to run forever.
- Standard codegolf rules and loopholes apply.
Example implementation:
> f <- function(string){
+ seed <- 1
+ set.seed(seed)
+ repeat{
+ seed <- seed + 1
+ set.seed(seed)
+ if(all(sample(letters, length(string), replace = TRUE) == string)){
+ break
+ }
+ }
+ generator <- function(){
+ set.seed(seed)
+ sample(letters, length(string), replace = TRUE)
+ }
+ return(generator)
+ }
> f("a")
function(){
set.seed(seed)
sample(letters, length(string), replace = TRUE)
}
<bytecode: 0x000000001c83f2d8>
<environment: 0x000000001f906c20>
> g=f("a")
> g()
[1] "a"
> g()
[1] "a"
> g()
[1] "a"