用固定种子混排文件的行? [英] Shuffling lines of a file with a fixed seed?
问题描述
我想用固定的种子重新排列文件的行,以便始终获得相同的随机顺序.我正在使用的命令如下:
I want to shuffle the lines of a file with a fixed seed so that I always get the same random order. The command I am using is as follows:
sort -R file.txt | head -200 > file.sff
我可以进行哪些更改,使其以固定的随机种子排序?
What change could I make it so that it sorts with a fixed random seed?
推荐答案
sort
的GNU实现具有一个--random-source
参数.将此参数与内容已知的文件名一起传递,将产生可靠的输出集.
The GNU implementation of sort
has a --random-source
argument. Passing this argument with the name of a file with known contents will result in a reliable set of output.
请参见随机来源 GNU coreutils手册中的文档,其中包含以下示例实现和示例:
See the Random sources documentation in the GNU coreutils manual, which contains the following sample implementation and example:
get_seeded_random()
{
seed="$1"
openssl enc -aes-256-ctr -pass pass:"$seed" -nosalt \
</dev/zero 2>/dev/null
}
shuf -i1-100 --random-source=<(get_seeded_random 42)
由于GNU sort
也是coreutils的一部分,因此相关文档也适用于此:
Since GNU sort
is also part of coreutils, the relevant documentation applies there as well:
sort --random-source=<(get_seeded_random 42) -R file.txt | head -200 > file.sff
这篇关于用固定种子混排文件的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!