我正在尝试使用 awk
和 rand()
对 csv 文件的第二列进行采样(任意数量的样本都可以)。但是,我注意到我总是得到相同数量的样本
cat toy.txt | awk -F',' 'rand()<0.2 {print $2}' | wc -l
我探索了一下,似乎 rand()
没有按我的预期工作。例如,下面的a似乎总是1,
cat toy.txt | awk -F',' 'a=rand() a<0.2 {print a}'
为什么?
最佳答案
来自 documentation :
CAUTION: In most awk implementations, including gawk, rand() starts generating numbers from the same starting number, or seed, each time you run awk. Thus, a program generates the same results each time you run it. The numbers are random within one awk run but predictable from run to run. This is convenient for debugging, but if you want a program to do different things each time it is used, you must change the seed to a value that is different in each run. To do this, use srand().
关于shell - rand() 如何在 awk 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45901042/