问题
I would like to shuffle output of find
BUT with a fixed seed, so that every time I run the command I get the same output.
Here's how I shuffle:
find . -name '*.wav' | shuf
The issue is that every time we have a new seed -> new order. My attempt to fix it:
find . -name '*.wav' | shuf --random-source=<(echo 42)
That works only on occasions (i.e. just a few cases, in a deterministic way). Most of the time it fails with:
shuf: ‘/proc/self/fd/12’: end of file
Same error is produced by e.g.
seq 1 100 | sort -R --random-source=<(echo 42)
That I have seen being used in other places.
This works though:
printf '%s\n' a b c | shuf --random-source=<(echo 42)
Why is that? And how I can fix it? I am open to any suggestions. Output of the find
is a part of a larger script. The solution should work for bash
and zsh
.
Why my solution did not work (EDIT)
Thanks to @franzisk and @Inian I think I understand now why my initial solution did not work. I was looking at --random-source
as it were a seek, while it is, well, "random source" = source of randomness. My echo 42
simply does not provide enough entropy for anything longer than a few lines. That's why it worked only in a couple of cases!
"Seeding" (as in: sending) large number of bytes does the job as it provides enough entropy.
回答1:
You can create your fixed_random function, using openssl to generate your random-source flow, like this
get_fixed_random()
{
openssl enc -aes-256-ctr -pass pass:"$1" -nosalt </dev/zero 2>/dev/null
}
Load the function into your environment
. /file-containing/get_fixed_random
Launch the find command, pipe the output to shuf using the random function to feed the --random-source option
find . -name '*.wav' | shuf --random-source=<(get_fixed_random 55)
NB: 55 is just the seed parameter passed. Change it to change the random result
来源:https://stackoverflow.com/questions/60266215/shuffle-output-of-find-with-fixed-seed