我有一个名为 random.awk
的 awk 脚本,它生成近似于正态分布的随机数
#!/usr/bin/awk -f
#the sum of three uniform [-1,1] random variables is used to get the approximate normal distribution
#the first argument the number of random numbers to be generated, the default is 100
#the second argument is to get the mean value, the default is 0
#the third argument is to get the standard deviation, the default is 1
function prand(){
return(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1));
}
BEGIN{
# establish default values
countnumber=(length(ARGV[1])==0?100:ARGV[1]);
meanvalue=(length(ARGV[2])==0?0.0:ARGV[2]);
stdeviation=(length(ARGV[3])==0?1.0:ARGV[3]);
for(i=0;i<count;i++){
printf("%.16e\n",prand()*stdeviation+meanvalue)
}
}
我如何创建一个 awk 文件以使用 random.awk
的输出作为输入并实现朴素求和和均值计算?
最佳答案
像您刚才那样创建一个 awk 脚本,用于生成随机值,但执行均值和求和计算,并使用 shell 管道符号 |
将第一个脚本的输出通过管道传输到第二个脚本。如果您需要更多帮助,则需要在问题中提供更多信息。
关于linux - 创建一个 awk 脚本来读取另一个 awk 文件的输出并实现总和和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658491/