用固定种子混排文件的行? [英] Shuffling lines of a file with a fixed seed?

查看:112
本文介绍了用固定种子混排文件的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用固定的种子重新排列文件的行,以便始终获得相同的随机顺序.我正在使用的命令如下:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆