用固定种子随机混合查找的输出 [英] Shuffle output of find with fixed seed

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

问题描述

我想用固定的种子对find BUT的输出进行混洗,以便每次我运行命令时都得到相同的输出.

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.

这是我洗牌的方式:

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":文件结尾

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.

这可以:

printf '%s\n' a b c | shuf --random-source=<(echo 42)

那是为什么?以及我该如何解决?我愿意接受任何建议. find的输出是较大脚本的一部分.该解决方案应该适用于bashzsh.

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.

由于@franzisk和@Inian,我想我现在明白为什么我的最初解决方案不起作用了.我一直在寻找--random-source,因为它是一种寻求,而"crandom source" =随机性的来源.我的echo 42根本没有为超过几行的任何时间提供足够的熵.这就是为什么它仅在少数情况下有效的原因!

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.

推荐答案

您可以创建您的fixed_random函数,使用openssl生成您的随机源流,就像这样

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

启动find命令,使用随机函数将输出通过管道传递给shuf,以提供--random-source选项

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只是传递的种子参数.更改以更改随机结果

NB: 55 is just the seed parameter passed. Change it to change the random result

这篇关于用固定种子随机混合查找的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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